Type alias DeepKeys<T>

DeepKeys<T>: { [ K in keyof Required<T>]: [K] | (T[K] extends object ? [K, ...DeepKeys<T[K]>] : never) }[keyof T]

Declares a type-safe path to the nested property.

Notice: making path through optional properties is not possible.

Example

Given a complex object structure:

interface Company {
id: number;
name: string;
address: Address;
}

interface Address {
country: string;
region: string;
street: Street;
building: string;
}

interface Street {
name: string;
type: string;
}

Use it to declare a type-safe tuple with a path to specific nested property:

const correctPath: DeepPath<Company> = ['address', 'street', 'name']; // ok
const wrongPath: DeepPath<Company> = ['address', 'street', 'kind']; // compilation error

Use it to declare a generic type-safe path:

export function get<T, Path extends DeepPath<T>>(source: T, path: Path): DeepType<T, Path> {
// ...
}

Use the function:

const streetName = get(company, ['address', 'street', 'name']);
const streetType = get(company, ['address', 'street', 'type']);
const region = get(company, ['address', 'region']);

Type Parameters

  • T

    Type of the whole.

Generated using TypeDoc