Closed stazz closed 7 months ago
Thanks for your feedback @stazz.
The problem is not caused by solid-router
, it is caused by TypeScript. In TypeScript you cannot use a specific interface
with a more general one. You can check this issue, and this answer for more information.
In your case, you cannot use the MyParams
interface as the generic type because it is more specific than the Record
type. Also, your suggestion won't work because object
means any object type like null
, array
, function
, and so on. So it doesn't guarantee type safety.
My recommendation for you is to use type
instead of interface
. This will solve the error from TypeScript.
Reviewing this I see that this is just a TS-ism I conferred with my experts and they said no action.
Describe the bug
The following TypeScript code results in compilation error:
The error is this:
And it is because the
useParams
looks like this:If the
Params
type definition would be changed fromRecord<string, string>
toobject
, then the error would go away.Your Example Website or App
https://example.com/not-relevant
Steps to Reproduce the Bug or Issue
I am using Typescript version
5.2.2
with the following configExpected behavior
As a user, I expect that using simple interface would not cause any errors.
There is a workaround for this problem:
Screenshots or Videos
No response
Platform
Irrelevant, as the bug is on compilation level.
Additional context
I want my routed component not to be too much aware of exact routing structure, therefore I want to expose the parameters it expects as explicit interface. This interface is then used as generic argument to
useParams
by the component, as well as in type assertions higher up in hierarchy where<Router>
and<Route>
components are used:This way, I get type-safe (in terms of
myComponentTypes.MyParams
) route path string as well as match filters.