Skip to content

Latest commit

 

History

History
55 lines (39 loc) · 990 Bytes

File metadata and controls

55 lines (39 loc) · 990 Bytes

typelab / utils / ObjectPathValue

type ObjectPathValue<T, K> = K extends "" ? T : K extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? Rest extends ObjectPath<T[Key]> ? ObjectPathValue<T[Key], Rest> : ObjectPath<T[Key]> : never : K extends keyof T ? T[K] : K extends `${infer Key extends number}` ? Key extends keyof T ? T[Key] : never : never;

Get the type of a property based on a given path.

Type Parameters

Type Parameter Description

T

The object type to extract value from.

K extends ObjectPath<T> | ""

The path string to navigate through the object.

Returns

The type of the property at the specified path.

Example

type Obj = { a: { b: number }; c: string };
type ValueType = ObjectPathValue<Obj, 'a.b'>; // number