You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Specifying undefined means that the route doesn't have params. A union type with undefined (e.g. SomeType | undefined) means that params are optional.
here it says SomeType | undefined, where it looks like its referring to a single param. but "params" is plural and seems to refer to all the route params.
this example shows that either a route takes no params, takes 1 required param or 1 optional param. but what if a route takes both required and optional params? in this case i cannot type the entire param object to be undefined
Argument of type '{ param1: string; }' is not assignable to parameter of type '{ param2: string | undefined; param1: string; }'.
Property 'param2' is missing in type '{ param1: string; }' but required in type '{ param2: string | undefined; param1: string; }'.ts(2345)
in my case, im setting param2 in SomeRoute with useLayoutEffect with setParams(), so i need to type it or else that shows in error.
Type '{ param2: any; }' is not assignable to type 'Partial<Readonly<{ param1: string; }>>'.
Object literal may only specify known properties, and 'param2' does not exist in type 'Partial<Readonly<{ param1: string; }>>'.ts(2322)
this error disappears when typing it like i did. but i still get the other error.
i have also tried setting the types as optional with a question mark. but this also shows an error if the screen takes no other params.
is it possible to define route params like this? where some are required while others are optional, not just the while params object?
The text was updated successfully, but these errors were encountered:
Adnan-Bacic
changed the title
typescript - type checking route params when some params are required wile other are optional
typescript - type checking route params when some params are required while other are optional
Feb 16, 2023
regarding: https://reactnavigation.org/docs/typescript/#type-checking-the-navigator
it says:
here it says
SomeType | undefined
, where it looks like its referring to a single param. but "params" is plural and seems to refer to all the route params.however, in the above example it shows:
this example shows that either a route takes no params, takes 1 required param or 1 optional param. but what if a route takes both required and optional params? in this case i cannot type the entire param object to be undefined
so something like this:
i tried this, however it still shows errors when not passing the optional params. so this code:
shows this error:
in my case, im setting
param2
inSomeRoute
withuseLayoutEffect
withsetParams()
, so i need to type it or else that shows in error.this error disappears when typing it like i did. but i still get the other error.
i have also tried setting the types as optional with a question mark. but this also shows an error if the screen takes no other params.
so this no longer shows an error:
but this does:
is it possible to define route params like this? where some are required while others are optional, not just the while params object?
The text was updated successfully, but these errors were encountered: