Skip to content

Commit 371053e

Browse files
committed
fix(routes): preserve checked routeUrl signatures
1 parent 0acf98f commit 371053e

3 files changed

Lines changed: 264 additions & 67 deletions

File tree

‎packages/void/package.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
"sandbox.Dockerfile"
2727
],
2828
"type": "module",
29+
"typesVersions": {
30+
"*": {
31+
"_route-url-types": [
32+
"./dist/runtime/routing/route-url.d.mts"
33+
]
34+
}
35+
},
2936
"imports": {
3037
"#self/_env": "./dist/runtime/env/runtime.mjs",
3138
"#self/email": "./dist/runtime/email/index.mjs",

‎packages/void/src/runtime/routing/route-url.ts‎

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,28 @@ type MissingRouteParam<P extends RoutePath, K extends string> = P extends RouteP
173173
? `Missing param ${K} for route ${P}. Pass it in { params: { ... } }.`
174174
: never
175175
: never;
176-
type MissingRouteParamKeys<P extends RoutePath, Params> = {
177-
[K in RoutePathMap[P] & string]: Params extends Record<K, unknown> ? never : K;
178-
}[RoutePathMap[P] & string];
176+
// An index signature permits a key without guaranteeing that it is present.
177+
type RequiredParamKeys<Params> = keyof {
178+
[K in keyof Params as string extends K ? never : {} extends Pick<Params, K> ? never : K]: unknown;
179+
};
180+
type MissingRouteParamKeys<P extends RoutePath, Params> = Exclude<
181+
RoutePathMap[P] & string,
182+
RequiredParamKeys<Params>
183+
>;
179184
type InvalidRouteParamKeys<P extends RoutePath, Params> = {
180-
[K in RoutePathMap[P] & string]: Params extends Record<K, string | number> ? never : K;
185+
[K in RoutePathMap[P] & string]: K extends keyof Params
186+
? [Params[K]] extends [string | number]
187+
? never
188+
: K
189+
: K;
181190
}[RoutePathMap[P] & string];
191+
type RequiredRouteParamError<P extends RoutePath, Params> = [
192+
MissingRouteParamKeys<P, Params>,
193+
] extends [never]
194+
? [InvalidRouteParamKeys<P, Params>] extends [never]
195+
? never
196+
: `Param ${InvalidRouteParamKeys<P, Params>} for route ${P} must be a string or number.`
197+
: MissingRouteParam<P, MissingRouteParamKeys<P, Params>>;
182198
type RouteUrlParamError<P extends RoutePath, O> = [RoutePathMap[P]] extends [never]
183199
? 'params' extends keyof O
184200
? [O['params']] extends [undefined]
@@ -187,18 +203,23 @@ type RouteUrlParamError<P extends RoutePath, O> = [RoutePathMap[P]] extends [nev
187203
: never
188204
: O extends { params: infer Params }
189205
? Params extends object
190-
? [Exclude<keyof Params, RoutePathMap[P]>] extends [never]
191-
? [MissingRouteParamKeys<P, Params>] extends [never]
192-
? [InvalidRouteParamKeys<P, Params>] extends [never]
193-
? never
194-
: `Param ${InvalidRouteParamKeys<P, Params>} for route ${P} must be a string or number.`
195-
: MissingRouteParam<P, MissingRouteParamKeys<P, Params>>
196-
: `Unknown param ${DiagnosticKeyName<Exclude<keyof Params, RoutePathMap[P]>>} for route ${P}. Use the parameter names in the route pattern.`
206+
? string extends keyof Params
207+
? RequiredRouteParamError<P, Params>
208+
: [Exclude<keyof Params, RoutePathMap[P]>] extends [never]
209+
? RequiredRouteParamError<P, Params>
210+
: `Unknown param ${DiagnosticKeyName<Exclude<keyof Params, RoutePathMap[P]>>} for route ${P}. Use the parameter names in the route pattern.`
197211
: `Params for route ${P} must be an object with the route's parameter names.`
198212
: MissingRouteParam<P, RoutePathMap[P] & string>;
199213
type RouteUrlQueryError<P extends RoutePath, Query> = [Query] extends [RouteQuery | undefined]
200214
? never
201-
: `Invalid query param ${DiagnosticKeyName<InvalidValueKeys<Exclude<Query, undefined>, RouteQueryValue | ReadonlyArray<RouteQueryValue>>>} for route ${P}. Use a string, number, boolean, null, undefined, or an array of these.`;
215+
: [
216+
InvalidValueKeys<
217+
Exclude<Query, undefined>,
218+
RouteQueryValue | ReadonlyArray<RouteQueryValue>
219+
>,
220+
] extends [never]
221+
? `Query for route ${P} must be an object with supported query values.`
222+
: `Invalid query param ${DiagnosticKeyName<InvalidValueKeys<Exclude<Query, undefined>, RouteQueryValue | ReadonlyArray<RouteQueryValue>>>} for route ${P}. Use a string, number, boolean, null, undefined, or an array of these.`;
202223
type RouteUrlOptionsError<P extends string, O> = [P] extends [RoutePath]
203224
? O extends undefined
204225
? MissingRouteParam<P, RoutePathMap[P] & string>
@@ -214,29 +235,29 @@ type RouteUrlOptionsError<P extends string, O> = [P] extends [RoutePath]
214235
| `Unknown routeUrl option: ${DiagnosticKeyName<Exclude<keyof O, 'params' | 'query' | 'hash'>>}. Use params, query, or hash.`
215236
: `Options for route ${P} must be an object with params, query, or hash.`
216237
: never;
238+
type RouteUrlCallArgs = [path: RoutePath | (string & {}), options?: unknown];
239+
type RouteUrlCallError<P extends string, O> = [P] extends [RoutePath]
240+
? [RouteUrlOptionsError<P, O>] extends [never]
241+
? `Invalid options for route ${P}. Pass an object with params, query, or hash.`
242+
: RouteUrlOptionsError<P, O>
243+
: string extends P
244+
? 'Pass a route pattern literal to routeUrl(); string is too broad.'
245+
: `Unknown route: ${P}. Pass a registered route pattern as the first argument.`;
217246
/** Build a relative URL from a known route pattern. */
218247
export function routeUrl<P extends RoutePath>(path: P, ...args: RouteUrlArgs<NoInfer<P>>): RouteUrl;
219-
// The default preserves the native tuple for explicit type arguments and
220-
// Parameters<typeof routeUrl<P>>. Inferred diagnostic tuples always require
221-
// an unsatisfied marker, even when an error calculation resolves to never.
222-
export function routeUrl<
223-
P extends RoutePath | (string & {}),
224-
A extends [options?: unknown] = never,
225-
>(
226-
path: P &
227-
(
228-
| RoutePath
229-
| UrlTypeError<
230-
string extends NoInfer<P>
231-
? 'Pass a route pattern literal to routeUrl(); string is too broad.'
232-
: `Unknown route: ${NoInfer<P>}. Pass a registered route pattern as the first argument.`
233-
>
234-
),
235-
...args: [A] extends [never]
236-
? RouteUrlArgs<NoInfer<P> & RoutePath>
248+
// Function helpers instantiate A at its constraint, where they must see the
249+
// native checked tuple. Inferred calls capture a specific tuple and require an
250+
// unsatisfied diagnostic marker. Both type arguments are required so <P> keeps
251+
// selecting the native overload, including in Parameters<typeof routeUrl<P>>.
252+
export function routeUrl<P extends RoutePath, const A extends RouteUrlCallArgs>(
253+
...args: RouteUrlCallArgs extends A
254+
? [path: P, ...args: RouteUrlArgs<NoInfer<P>>]
237255
: A &
238-
[options?: RouteUrlOptionsInput<Extract<NoInfer<P>, RoutePath>>] &
239-
UrlTypeError<RouteUrlOptionsError<NoInfer<P>, NoInfer<A>[0]>>
256+
[
257+
path: RoutePath | (string & {}),
258+
options?: RouteUrlOptionsInput<Extract<NoInfer<A>[0], RoutePath>>,
259+
] &
260+
UrlTypeError<RouteUrlCallError<NoInfer<A>[0], NoInfer<A>[1]>>
240261
): RouteUrl;
241262
export function routeUrl(path: string, ...args: [options?: unknown]): RouteUrl {
242263
return buildRouteUrl(path, args[0] as UrlOptions | undefined) as RouteUrl;

0 commit comments

Comments
 (0)