You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
843 B

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
)];