Created
September 18, 2022 15:37
-
-
Save fostyfost/5027a3d6969dd2d7d57c1fcbabe158e9 to your computer and use it in GitHub Desktop.
Extract Type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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