Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types in useHistory and useRouter are incorrect #280

Closed
Curstantine opened this issue Feb 4, 2024 · 7 comments · Fixed by #281
Closed

Types in useHistory and useRouter are incorrect #280

Curstantine opened this issue Feb 4, 2024 · 7 comments · Fixed by #281

Comments

@Curstantine
Copy link

Curstantine commented Feb 4, 2024

The contexts.d.ts file defines useHistory, useRouter as readable when they are objects with functions attached to them.

More context:
Since these don't have a subscribe method attached to it, you can't use these functions with svelte's subscribe syntax. And the messy types are annoying to work with.

image

@Curstantine Curstantine changed the title Types in useHistory, useRouter is incorrect Types inuseHistory and useRouter are incorrect Feb 5, 2024
@Curstantine Curstantine changed the title Types inuseHistory and useRouter are incorrect Types in useHistory and useRouter are incorrect Feb 5, 2024
@krishnaTORQUE
Copy link
Collaborator

Please raise a PR if you have better option.
Thanks.

@james-em
Copy link
Contributor

james-em commented Feb 9, 2024

@krishnaTORQUE

They are indeed wrong:

Test

const router = useRouter();
$: console.log(router)

Output

{activeRoute: {…}, base: {…}, routerBase: {…}, registerRoute: ƒ, unregisterRoute: ƒ}

What types say

type ROUTER = RouterProps;
export const useRouter: () => ReturnType<typeof readable<ROUTER>>;

type RouterProps = {
    basepath?: string;
    url?: string;
    viewtransition?: (direction?: string) => Viewtransition;
};

It's not really that the given option is not enough, it simply is incorrect.

RouterProps is the proper type for the props of <Router ...> component but not the useRouter

@james-em
Copy link
Contributor

james-em commented Feb 9, 2024

@krishnaTORQUE

PR: #281

@james-em
Copy link
Contributor

james-em commented Feb 9, 2024

@krishnaTORQUE

Thanks for merging. Hopefully it will be released soon! :)

I forgot to mention one thing

registerRoute: (route: Omit<RouteConfig, 'default'>) => {};
unregisterRouter: () => {}

registerRoute is properly typed but not unregisterRouter. However, there is an issue with it because internally it compares using === for exactly same route passed to registerRoute but the route is re-initialized by registerRoute internally making it impossible to have a working unregisterRouter

It would require some internal changes to do something like this:

const myRoute = ...
const registeredRoute = registerRoute(myRoute); // Currently returns nothing

// Let's say it would return something
myRoute === registeredRoute
=> false

unregisterRouter(myRoute) // Would fail
unregisterRouter(registeredRoute) // Would work

Just though I should let you know

@krishnaTORQUE
Copy link
Collaborator

@krishnaTORQUE

Thanks for merging. Hopefully it will be released soon! :)

I forgot to mention one thing

registerRoute: (route: Omit<RouteConfig, 'default'>) => {};
unregisterRouter: () => {}

registerRoute is properly typed but not unregisterRouter. However, there is an issue with it because internally it compares using === for exactly same route passed to registerRoute but the route is re-initialized by registerRoute internally making it impossible to have a working unregisterRouter

It would require some internal changes to do something like this:

const myRoute = ...
const registeredRoute = registerRoute(myRoute); // Currently returns nothing

// Let's say it would return something
myRoute === registeredRoute
=> false

unregisterRouter(myRoute) // Would fail
unregisterRouter(registeredRoute) // Would work

Thanks for the PR.
If there is any other changes require, you can raise PR for that as well.

@krishnaTORQUE
Copy link
Collaborator

Hi @james-em
Waiting for your response.

@james-em
Copy link
Contributor

Hi @james-em Waiting for your response.

Thanks for getting back. I do not need to call registerRoute or unregisterRoute for my usage. Just though I should let you know they are not usuable for now outside of the "expected" usage described in the README.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants