Skip to content

Instantly share code, notes, and snippets.

@garbles
Created May 9, 2018 23:24
Show Gist options
  • Save garbles/1d1b8f7465cce2877a5039826d086c27 to your computer and use it in GitHub Desktop.
Save garbles/1d1b8f7465cce2877a5039826d086c27 to your computer and use it in GitHub Desktop.
TypeScript utils for 2.8
type GetDeepType1<T, A> = A extends [infer B] ? B extends keyof T ? T[B] : undefined : undefined;
type GetDeepType2<T, A> = A extends [infer B, infer C] ? B extends keyof T ? GetDeepType1<T[B], [C]> : undefined : undefined;
type GetDeepType3<T, A> = A extends [infer B, infer C, infer D] ? B extends keyof T ? GetDeepType2<T[B], [C, D]> : undefined : undefined;
type GetDeepType4<T, A> = A extends [infer B, infer C, infer D, infer E] ? B extends keyof T ? GetDeepType3<T[B], [C, D, E]> : undefined : undefined;
type GetDeepType5<T, A> = A extends [infer B, infer C, infer D, infer E, infer F] ? B extends keyof T ? GetDeepType4<T[B], [C, D, E, F]> : undefined : undefined;
type GetDeepType6<T, A> = A extends [infer B, infer C, infer D, infer E, infer F, infer G] ? B extends keyof T ? GetDeepType5<T[B], [C, D, E, F, G]> : undefined : undefined;
type GetDeepType<T, A> =
A extends [any] ? GetDeepType1<T, A> :
A extends [any, any] ? GetDeepType2<T, A> :
A extends [any, any, any] ? GetDeepType3<T, A> :
A extends [any, any, any, any] ? GetDeepType4<T, A> :
A extends [any, any, any, any, any] ? GetDeepType5<T, A> :
A extends [any, any, any, any, any, any] ? GetDeepType6<T, A> :
never;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment