Tree path array
Challenge
Create a type Path that represents validates a possible path of a tree under the form of an array.
Related challenges:
declare const example: {
foo: {
bar: {
a: string;
};
baz: {
b: number;
c: number;
};
};
};
/* Possible solutions:
[]
['foo']
['foo', 'bar']
['foo', 'bar', 'a']
['foo', 'baz']
['foo', 'baz', 'b']
['foo', 'baz', 'c']
*/
Solution
type Path<T, K extends keyof T = keyof T> = K extends unknown
? T[K] extends Record<any, unknown>
? [K] | [K, ...Path<T[K]>]
: [K]
: never;