Tree path array
约 169 字小于 1 分钟
2026-02-11
题目
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']解题思路
待补充
答案
type Path<T> = any验证
import type { ExpectExtends, ExpectFalse, ExpectTrue } from '@type-challenges/utils'
declare const example: {
foo: {
bar: {
a: string
}
baz: {
b: number
c: number
}
}
}
type cases = [
ExpectTrue<ExpectExtends<Path<typeof example['foo']['bar']>, ['a']>>,
ExpectTrue<ExpectExtends<Path<typeof example['foo']['baz']>, ['b'] | ['c'] >>,
ExpectTrue<ExpectExtends<Path<typeof example['foo']>, ['bar'] | ['baz'] | ['bar', 'a'] | ['baz', 'b'] | ['baz', 'c']>>,
ExpectFalse<ExpectExtends<Path<typeof example['foo']['bar']>, ['z']>>,
]参考
无
