1- import { Narrow } from './type-utils' ;
2-
31/**
42 * The path with colon-prefixed parameters
53 * e.g. "/posts/:id".
@@ -37,6 +35,19 @@ export type AppRouteMutation = {
3735 responses : Record < number , unknown > ;
3836} ;
3937
38+ /**
39+ * Recursively process a router, allowing for you to define nested routers.
40+ *
41+ * The main purpose of this is to convert all path strings into string constants so we can infer the path
42+ */
43+ type RecursivelyProcessAppRouter < T extends AppRouter > = {
44+ [ K in keyof T ] : T [ K ] extends AppRoute
45+ ? T [ K ]
46+ : T [ K ] extends AppRouter
47+ ? RecursivelyProcessAppRouter < T [ K ] >
48+ : T [ K ] ;
49+ } ;
50+
4051/**
4152 * A union of all possible endpoint types.
4253 */
@@ -67,17 +78,17 @@ type ContractInstance = {
6778 /**
6879 * A collection of routes or routers
6980 */
70- router : < T extends AppRouter > ( endpoints : Narrow < T > ) => T ;
81+ router : < T extends AppRouter > ( endpoints : RecursivelyProcessAppRouter < T > ) => T ;
7182 /**
7283 * A single query route, should exist within
7384 * a {@link AppRouter}
7485 */
75- query : < T extends AppRouteQuery > ( query : Narrow < T > ) => T ;
86+ query : < T extends AppRouteQuery > ( query : T ) => T ;
7687 /**
7788 * A single mutation route, should exist within
7889 * a {@link AppRouter}
7990 */
80- mutation : < T extends AppRouteMutation > ( mutation : Narrow < T > ) => T ;
91+ mutation : < T extends AppRouteMutation > ( mutation : T ) => T ;
8192 /**
8293 * Exists to allow storing a Type in the contract (at compile time only)
8394 */
@@ -103,9 +114,7 @@ export const initContract = (): ContractInstance => {
103114 return {
104115 // @ts -expect-error - this is a type error, but it's not clear how to fix it
105116 router : ( args ) => args ,
106- // @ts -expect-error - this is a type error, but it's not clear how to fix it
107117 query : ( args ) => args ,
108- // @ts -expect-error - this is a type error, but it's not clear how to fix it
109118 mutation : ( args ) => args ,
110119 response : < T > ( ) => undefined as unknown as T ,
111120 body : < T > ( ) => undefined as unknown as T ,
0 commit comments