Tree path array
题目
Create a type Path
that represents validates a possible path of a tree under the form of an array.
Related challenges:
ts
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']
解答
ts
type Path<T, P extends PropertyKey[] = []> =
| P
| { [K in keyof T]: Path<T[K], [...P, K]> }[keyof T];