@@ -198,11 +198,115 @@ export function generateRouteTypes(
198198 ) ;
199199 lines . push ( ' : string;' ) ;
200200 lines . push ( ' export type RouteUrlInput = Href | URL | RouteDestination;' ) ;
201- lines . push ( ' export function routeUrl<P extends RoutePath>(' ) ;
202- lines . push ( ' path: P,' ) ;
203- lines . push ( ' ...args: [RoutePathMap[NoInfer<P>]] extends [never]' ) ;
204- lines . push ( ' ? [options?: RouteOptions<P>]' ) ;
205- lines . push ( ' : [options: RouteOptions<P>]' ) ;
201+ lines . push ( ' // Keep route/option shapes visible to editor completion while inferring the' ) ;
202+ lines . push ( " // caller's input separately, so invalid calls can explain the correction." ) ;
203+ lines . push (
204+ ' type RouteUrlError<Message extends string> = { readonly __routeUrlError: Message };' ,
205+ ) ;
206+ lines . push ( ' type RouteUrlOptionsInput<P extends RoutePath> = {' ) ;
207+ lines . push (
208+ ' params?: { [K in RoutePathMap[P] & string]?: unknown } | Record<string, unknown>;' ,
209+ ) ;
210+ lines . push ( ' query?: Record<string, unknown>;' ) ;
211+ lines . push ( ' hash?: unknown;' ) ;
212+ lines . push ( ' };' ) ;
213+ lines . push ( ' type RouteUrlArgs<P extends RoutePath> = [RoutePathMap[P]] extends [never]' ) ;
214+ lines . push ( ' ? [options?: RouteOptions<P>]' ) ;
215+ lines . push ( ' : [options: RouteOptions<P>];' ) ;
216+ lines . push (
217+ ' type MissingRouteParam<P extends RoutePath, K extends string> = P extends RoutePath' ,
218+ ) ;
219+ lines . push ( ' ? K extends RoutePathMap[P]' ) ;
220+ lines . push ( ' ? `Missing param ${K} for route ${P}. Pass it in { params: { ... } }.`' ) ;
221+ lines . push ( ' : never' ) ;
222+ lines . push ( ' : never;' ) ;
223+ lines . push ( ' type MissingRouteParamKeys<P extends RoutePath, Params> = {' ) ;
224+ lines . push ( ' [K in RoutePathMap[P] & string]: Params extends Record<K, unknown> ? never : K;' ) ;
225+ lines . push ( ' }[RoutePathMap[P] & string];' ) ;
226+ lines . push ( ' type InvalidRouteParamKeys<P extends RoutePath, Params> = {' ) ;
227+ lines . push (
228+ ' [K in RoutePathMap[P] & string]: Params extends Record<K, string | number> ? never : K;' ,
229+ ) ;
230+ lines . push ( ' }[RoutePathMap[P] & string];' ) ;
231+ lines . push ( ' type InvalidRouteValueKeys<Values, Allowed> = {' ) ;
232+ lines . push ( ' [K in keyof Values]-?: [Values[K]] extends [Allowed] ? never : K;' ) ;
233+ lines . push ( ' }[keyof Values] &' ) ;
234+ lines . push ( ' string;' ) ;
235+ lines . push (
236+ ' type RouteUrlParamError<P extends RoutePath, O> = [RoutePathMap[P]] extends [never]' ,
237+ ) ;
238+ lines . push ( " ? 'params' extends keyof O" ) ;
239+ lines . push ( " ? [O['params']] extends [undefined]" ) ;
240+ lines . push ( ' ? never' ) ;
241+ lines . push ( ' : `Route ${P} has no path params. Remove params.`' ) ;
242+ lines . push ( ' : never' ) ;
243+ lines . push ( ' : O extends { params: infer Params }' ) ;
244+ lines . push ( ' ? Params extends object' ) ;
245+ lines . push ( ' ? [Exclude<keyof Params, RoutePathMap[P]>] extends [never]' ) ;
246+ lines . push ( ' ? [MissingRouteParamKeys<P, Params>] extends [never]' ) ;
247+ lines . push ( ' ? [InvalidRouteParamKeys<P, Params>] extends [never]' ) ;
248+ lines . push ( ' ? never' ) ;
249+ lines . push (
250+ ' : `Param ${InvalidRouteParamKeys<P, Params>} for route ${P} must be a string or number.`' ,
251+ ) ;
252+ lines . push ( ' : MissingRouteParam<P, MissingRouteParamKeys<P, Params>>' ) ;
253+ lines . push (
254+ ' : `Unknown param ${Exclude<keyof Params, RoutePathMap[P]> & string} for route ${P}. Use the parameter names in the route pattern.`' ,
255+ ) ;
256+ lines . push (
257+ " : `Params for route ${P} must be an object with the route's parameter names.`" ,
258+ ) ;
259+ lines . push ( ' : MissingRouteParam<P, RoutePathMap[P] & string>;' ) ;
260+ lines . push (
261+ ' type RouteUrlQueryError<P extends RoutePath, Query> = [Query] extends [RouteQuery | undefined]' ,
262+ ) ;
263+ lines . push ( ' ? never' ) ;
264+ lines . push (
265+ ' : `Invalid query param ${InvalidRouteValueKeys<Exclude<Query, undefined>, RouteQueryValue | ReadonlyArray<RouteQueryValue>>} for route ${P}. Use a string, number, boolean, null, undefined, or an array of these.`;' ,
266+ ) ;
267+ lines . push ( ' type RouteUrlOptionsError<P extends string, O> = [P] extends [RoutePath]' ) ;
268+ lines . push ( ' ? O extends undefined' ) ;
269+ lines . push ( ' ? MissingRouteParam<P, RoutePathMap[P] & string>' ) ;
270+ lines . push ( ' : O extends object' ) ;
271+ lines . push ( ' ?' ) ;
272+ lines . push ( ' | RouteUrlParamError<P, O>' ) ;
273+ lines . push ( " | ('query' extends keyof O ? RouteUrlQueryError<P, O['query']> : never)" ) ;
274+ lines . push ( " | ('hash' extends keyof O" ) ;
275+ lines . push ( " ? [O['hash']] extends [string | undefined]" ) ;
276+ lines . push ( ' ? never' ) ;
277+ lines . push ( ' : `Hash for route ${P} must be a string.`' ) ;
278+ lines . push ( ' : never)' ) ;
279+ lines . push (
280+ " | (Exclude<keyof O, 'params' | 'query' | 'hash'> extends infer K extends string" ,
281+ ) ;
282+ lines . push ( ' ? `Unknown routeUrl option: ${K}. Use params, query, or hash.`' ) ;
283+ lines . push ( ' : never)' ) ;
284+ lines . push ( ' : `Options for route ${P} must be an object with params, query, or hash.`' ) ;
285+ lines . push ( ' : never;' ) ;
286+ lines . push ( ' type ValidateRouteUrlArgs<P extends string, A extends [options?: unknown]> = [' ) ;
287+ lines . push ( ' RouteUrlOptionsError<P, A[0]>,' ) ;
288+ lines . push ( ' ] extends [never]' ) ;
289+ lines . push ( ' ? unknown' ) ;
290+ lines . push ( ' : RouteUrlOptionsError<P, A[0]>;' ) ;
291+ lines . push ( '' ) ;
292+ lines . push ( ' export function routeUrl<' ) ;
293+ lines . push ( ' P extends RoutePath | (string & {}),' ) ;
294+ lines . push ( ' A extends [options?: unknown] = RouteUrlArgs<Extract<P, RoutePath>>,' ) ;
295+ lines . push ( ' >(' ) ;
296+ lines . push ( ' path: P &' ) ;
297+ lines . push ( ' (' ) ;
298+ lines . push ( ' | RoutePath' ) ;
299+ lines . push ( ' | RouteUrlError<' ) ;
300+ lines . push ( ' string extends NoInfer<P>' ) ;
301+ lines . push ( " ? 'Pass a route pattern literal to routeUrl(); string is too broad.'" ) ;
302+ lines . push (
303+ ' : `Unknown route: ${NoInfer<P>}. Pass a registered route pattern as the first argument.`' ,
304+ ) ;
305+ lines . push ( ' >' ) ;
306+ lines . push ( ' ),' ) ;
307+ lines . push ( ' ...args: A &' ) ;
308+ lines . push ( ' [options?: RouteUrlOptionsInput<Extract<NoInfer<P>, RoutePath>>] &' ) ;
309+ lines . push ( ' ValidateRouteUrlArgs<NoInfer<P>, NoInfer<A>>' ) ;
206310 lines . push ( ' ): RouteUrl;' ) ;
207311 lines . push ( '' ) ;
208312 lines . push ( ' export interface RouteTypesConfig {' ) ;
0 commit comments