types-kit.deepget.md
September 4, 2023 ยท View on GitHub
DeepGet type
Get the deep specified value from T.
Signature:
export type DeepGet<
T,
K extends DeepKeys<T>,
> = K extends `${infer Head}.${infer Tail}`
? Head extends Keys<T>
? T[Head] extends infer V
? V extends V
? IsObject<V> extends true
? Tail extends DeepKeys<V>
? DeepGet<V, Tail>
: never
: // if the value of the parent property is not an object, like a?.b, then the value of b will be undefined
undefined
: never
: never
: never
: K extends keyof T
? T[K]
: never
References: DeepKeys, Keys, IsObject, DeepGet
Example
interface Props {
a: {
d: () => void
}
b: string
c: boolean
}
// Expect: (()=> void) | number | string
type PropValues = DeepGet<Props, 'a.d' | 'b'>