#How to type router params in loader function ?
1 messages ยท Page 1 of 1 (latest)
it should be string | undefined, no?
Yes that's the type I am getting but why would it be undefined ? sorry if its a dumb question
Good question
I don't know for sure I'm guessing maybe there's some edge case where you have a route like /movies/$id but someone manages to visit the route without filling in the param
let me have a play
Alright thank you ๐
#react-router-general message
So it might be undefined because TypeScript doesn't know whether the param you're trying to get is actually valid
obviously you know that params.movieId is a string because you have a file called $movieId
but typescript can't tell that
so if you were to try and do, say, params.songId instead, it needs to return undefined there because that isn't a valid param for that route
you can probably quite safely cast it to a string if you wanted to
A couple other options is using invariant
https://remix.run/docs/en/v1/tutorials/blog#dynamic-route-params