Skip to content

Instantly share code, notes, and snippets.

@fostyfost
Created September 18, 2022 15:37
Show Gist options
  • Save fostyfost/5027a3d6969dd2d7d57c1fcbabe158e9 to your computer and use it in GitHub Desktop.
Save fostyfost/5027a3d6969dd2d7d57c1fcbabe158e9 to your computer and use it in GitHub Desktop.
Extract Type
// @see https://stackoverflow.com/questions/60449479/typescript-extract-type-from-object-by-object-path-in-tuple
type ExtractType<O, T extends Array<any>> = {
[K in keyof O]: ((...a: T) => any) extends (a: any, ...args: infer Tail) => any
? Tail['length'] extends 0
? O[K]
: ExtractType<O[K], Tail>
: never
}[T[0]]
// example Foo is number
type Foo = ExtractType<{ a: { b: { c: { d: number } } }; g: string }, ['a', 'b', 'c', 'd']>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment