I know this question has been asked before, and that there are working approaches, such as this, but I have a specific question about why a certain implementation using mapped types does not work:
When trying to recursively get all keys of a nested object, you need to handle the case of an array, so I wrote this type:
type KeysDeep<T> = T extends object
? {
[K in keyof T]-?:
| K
| (T[K] extends (infer A)[] ? KeysDeep<A> : KeysDeep<T[K]>);
}[keyof T]
: never;
And this seems to work well, except if a property with an array type is optional, e.g.
type Foo = {
foo: string;
}
type Bar = {
bar: string;
foos: Foo[];
}
type Baz = {
baz: string;
maybeFoos?: Foo[];
}
type Good = KeysDeep<Bar>; // "foos" | "bar" | "foo"
type Bad = KeyDeep<Baz>; // "foos" | "bar" | "foo" | ... a zillion more
I would have thought the ?- operator would have adequately handled the optional property here. I've tried modifying this type in several ways but have been unable to make it work. Is there any way to fix this issue?
In the end, it's fine to use the solution I linked above, however it has poorer intellisense. e.g. the intellisense tooltip will list something like "foo" | keyof Bar instead of "foos" | "bar" | "foo".
Anyway, this isn't a huge deal, but I'm just curious if there's a fix and as to why this doesn't work. Thanks in advance.
Here's a playground link with a full example.