parent
1136e6c59e
commit
a6a4d76de9
@ -0,0 +1,33 @@
|
||||
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']>>,
|
||||
];
|
||||
|
||||
type Path<
|
||||
T extends Record<PropertyKey, unknown>,
|
||||
Keys extends keyof T = keyof T,
|
||||
> =
|
||||
Keys extends never
|
||||
? never
|
||||
// This is the fancy part
|
||||
: [Keys] | [Keys, ...(
|
||||
T[Keys] extends Record<string, unknown>
|
||||
? Path<T[Keys]>
|
||||
: never
|
||||
)];
|
||||
Loading…
Reference in new issue