33345
Dynamic Route
type InverseString<T extends string, U extends string = ''> =T['length'] extends 0 ? U : T extends `${infer First}${infer Rest}` ? InverseString<Rest, `${First}${U}`> : U;
type RemoveFirst<T extends string> = T extends `${infer First}${infer Rest}` ? Rest : T;
type RemoveLastSlash<T extends string> = InverseString<T> extends `/${infer Rest}` ? RemoveLastSlash<InverseString<RemoveFirst<InverseString<T>>>> : T;
type SlashSplitted<T extends string, U extends string[]=[]> = T['length']extends 0?U: T extends `/${infer First}/${infer Rest}` ? SlashSplitted<`/${Rest}`, [...U, First]> : T extends `/${infer Rest}` ? [...U, Rest] : U;
type AddFirstSlash<T extends string> = T extends `/${infer Rest}` ? T : `/${T}`
type IsNever<T> = [T] extends [never] ? true : false;
type Clear<T extends string> = T extends `[[...${infer Key}]]` ? NullableRecord<Record<Key,string[]>> :
T extends `[...${infer Key}]` ? Key extends '' ? Record<'...',string> : Record<Key,string[]> :
T extends `[${infer Key}]` ? Record<Key,string> :
never;
type CanSlugArrayFilter<T extends string[], U extends string[]=[]> = T['length'] extends 0 ? U :
T extends [infer First extends string, ...infer Rest extends string[]] ? CanSlugArrayFilter<Rest, [...U, CanSlug<First>]> : U;
type CanSlug<T extends string> =
T extends `[[...${infer Key}]]` ? T:
T extends `[...${infer Key}]` ? T :
T extends `[${infer Key}]` ? Key extends '' ? never : T
: never;
type NeverFilter<T extends any[],U extends any[]=[]> = T['length'] extends 0 ? U :
T extends [infer First, ...infer Rest] ? IsNever<First> extends true ? NeverFilter<Rest, U> : NeverFilter<Rest,[...U, First]> : U;
type ApplySlugTypes<T extends string[], X extends {[key in string]?:string|string[]}={}> = T['length'] extends 0 ? X :
T extends [infer First extends string, ...infer Rest extends string[]] ? ApplySlugTypes<Rest, Clear<First> & X> : X
type NullableRecord<T extends object>={[Key in keyof T]?:T[Key]};
type IsValidSlugRoute<T extends string>=
T['length'] extends 0 ? true :
T extends `[${`[...${infer F}]`|`...${infer F}`}]/[${`[...${infer L}]`|`...${infer L}`}]${infer Rest}`
? `${F}${L}` extends `${F|L}`
? T extends `${infer First}${infer Rest}` ? IsValidSlugRoute<Rest> : true
: false
: T extends `[${`[...${infer F}]`|`...${infer F}`}]/[${infer M}]/[${`[...${infer L}]`|`...${infer L}`}]${infer Rest}`
? `${F}${M}${L}` extends `${F}${L}` |`${M}${L}` |`${F}${M}`
? T extends `${infer First}${infer Rest}` ? IsValidSlugRoute<Rest> : true
: false
: T extends `${infer First}${infer Rest}` ? IsValidSlugRoute<Rest> : true;
type Calibrate<T extends string> = RemoveLastSlash<AddFirstSlash<T>>
type RecordToObject<T extends {}>={[Key in keyof T]:T[Key]}
type DynamicRoute<T extends string> =
IsValidSlugRoute<T> extends true
? RecordToObject<ApplySlugTypes<NeverFilter<CanSlugArrayFilter<SlashSplitted<Calibrate<T>>>>>>
: never