ferdy
ferdy
Explore posts from servers
TTCTheo's Typesafe Cult
Created by ferdy on 4/24/2023 in #questions
Nested keyof
Hi all, I am trying to use a fancy type to get a nested keyof effect. I found this code snippet online:
type NestedKeyOf<ObjectType extends object> = {
[Key in keyof ObjectType & (string | number)]: ObjectType[Key] extends object
? `${Key}.${NestedKeyOf<ObjectType[Key]>}`
: `${Key}`;
}[keyof ObjectType & (string | number)];
type NestedKeyOf<ObjectType extends object> = {
[Key in keyof ObjectType & (string | number)]: ObjectType[Key] extends object
? `${Key}.${NestedKeyOf<ObjectType[Key]>}`
: `${Key}`;
}[keyof ObjectType & (string | number)];
and when using it on the declaration side it definitely works, but I am having trouble using it to index into the type. here's a link to the code snippet: https://www.typescriptlang.org/play?#code/C4TwDgpgBAchDOwIBMDSEQHkBmAeTARgFYQDGwAKuNBAB5IB2y8UA9sWcAHxQC8UAbwBQUKAG10IKAEsGUANYZW2KIRLkqkKADIoACkQAnWQHMoAHygMArgFsCEQwEoAugC5VHDdQkYXUOkZmNi9gEVEoAH4oAAMAEgFJAF8AOgS4RBRJHHxQzQhfEBcuJJjw0Q94xIxSgG4hJLFFEGVPdUpqHX0jUwsrOwdnF3qhUC0AMVZWPkFyqAIAQ0MPYQi1+YWALw8ehhM5pIAaOewpneBjPYahIVJWBkQFDA8MpDQMHMnWHn4AIkXDClFptfiM7g9gFBTqwPF8ZqsIgCVsCPP8tr8jiczlBftDfg0oAsWODEGD7o9DAhrAAbSH8aFNPy1IA
2 replies