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
In more complex scenarios, URL paths may come from dynamic sources, e.g. a backend response. In these situations, the data is likely untyped, or loosely typed, where it's unreasonable to keep the external source synchronised with the possible routes.
For example:
constres=awaitfetch("https://my.links")// return a list of linksconstlinks=(awaitres.json()).links;router.navigate({to: links[0].to,// no guarantee this route exists});
A type guard would be able to provide a utility to guarantee that a runtime object satisfies our discrete set of navigation options.
functionisValidToOptions<T>(opts: T)T is ToOptions{// logic in here to check against registered routes}// usageif(isValidToOptions(dynamicLinkResponse)){navigate(dynamicLinkResponse);// type safe}
Alternatively, it could throw an error, rather than using the is syntax, and return the valid ToOptions
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In more complex scenarios, URL paths may come from dynamic sources, e.g. a backend response. In these situations, the data is likely untyped, or loosely typed, where it's unreasonable to keep the external source synchronised with the possible routes.
For example:
A type guard would be able to provide a utility to guarantee that a runtime object satisfies our discrete set of navigation options.
Alternatively, it could throw an error, rather than using the
is
syntax, and return the validToOptions
Beta Was this translation helpful? Give feedback.
All reactions