Is it possible to extract type form object providing a path to it in N size tuple format. If so, how? Example what I have in mind:
type ExtractType<O, T> = ...;
type Foo = ExtractType<{ a: { b: number } }, ['a', 'b']> // I expect Foo type to be number
Below naive version, naive it means I don't restrict path to be valid path of the object, if keys path is invalid the return type will be unknown.
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']>
It is recursive type so depth is not restricted. Few things to consider:
((...a: T) => any) extends ((a: any, ...args: infer Tail) => any) this part is used in order to cut the Head of the tuple, and work recursively with Tail. It means we traverse the object until tuple with keys will be empty. Every recursive call work with one element less.Tail['length'] extends 0 ? O[K] : ExtractType<O[K], Tail> - we take the value type only when we are in the end of the key tupleIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With