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
Hello, loving this project so far! We think this will help keep our react-router paths under control our large SPA app 🙏
When attempting to use the library, I noticed that type enforcement for the last keys did not behave as expected:
import{arg,createRouting,segment}from'ts-routes';exportconstnavRoutes=createRouting({businessId: {
...segment`/${arg('identityBusinessId')}`,children: {dashboard: segment`/dashboard`,},},});constrouteExample1=navRoutes.businessId.invalidKey({identityBusinessId: 'id'});// type error on `invalidKey` as expectedconstrouteExample2=navRoutes.businessId.dashboard.invalidKey({identityBusinessId: 'id'});// no type errorsconstrouteExample3=navRoutes.businessId.dashboard.invalidKey1.invalidKey2({identityBusinessId: 'id'});// type error on `invalidKey2`, but not `invalidKey1`
I would expect (ideally) that use of invalidKey anywhere would throw a type error. Is there anything I'm missing in the routing implementation?
Getting past this issue would allow us to use this library to type enforce our react-router paths which would be a huge value add for our site stability.
Thanks in advance! 🙏
The text was updated successfully, but these errors were encountered:
Just wanted to post an update on this. By wrapping segment and using it inside our createRouting call, I was able to improve the type enforcement around the nested keys. Specifically, setting the children in the segment to {} and following the destructure pattern in the docs, type checking started enforcing their usage correctly:
import{segment}from'ts-routes';classPathParamDescription<TNameextendsstring=string,TOptionalityextends'required'|'optional'='optional'>{readonlypattern: string;readonlyname: TName;readonlyoptionality: TOptionality;constructor({
name,
optionality,
pattern,}: {name: TName;optionality: TOptionality;pattern?: string;}){constpatternPart=pattern ? `(${pattern})` : '';constrequirementPart=optionality==='optional' ? '?' : '';this.name=name;this.optionality=optionality;this.pattern=`:${name}${patternPart}${requirementPart}`;}}/** * A wrapper for `segment` from `ts-routes`, based on the original implementation * * By including `children: {}` in the return object, we improve type enforcement for nested routes. * See original implementation: https://github.com/leancodepl/ts-routes/blob/f25bcf95213e0acebca0813144bdb7ae61abf5af/src/segment.ts */exportconstwrappedSegment=<TPathParamsDescriptionextendsPathParamDescription<string,'required'|'optional'>[]>(literals: TemplateStringsArray,
...placeholders: TPathParamsDescription)=>{return{
...segment(literals, ...placeholders),children: {},};};
Hey, happy to see that you've found the solution. Your problem definitely looks like a bug inside the library, we'll try to have it fixed soon so that you won't have to do any workarounds on your side.
Hello, loving this project so far! We think this will help keep our
react-router
paths under control our large SPA app 🙏When attempting to use the library, I noticed that type enforcement for the last keys did not behave as expected:
I would expect (ideally) that use of
invalidKey
anywhere would throw a type error. Is there anything I'm missing in the routing implementation?Getting past this issue would allow us to use this library to type enforce our
react-router
paths which would be a huge value add for our site stability.Thanks in advance! 🙏
The text was updated successfully, but these errors were encountered: