I am attempting to retrieve the appropriate typescript types using dot notation, excluding the deepest child.
After some searching, I came across a helpful resource at Typescript string dot notation of nested object, which partially solved my issue. However, it does not cover omitting the deepest child.
For example:
type TranslationType = {
key1: {
en: string,
}
sub: {
key2: {
en: string,
}
}
}
type PathsToStringProps<T> = T extends string
? []
: {
[K in Extract<keyof T, string>]: [K, ...PathsToStringProps<T[K]>]
}[Extract<keyof T, string>]
type Join<T extends string[]> = T extends []
? never
: T extends [infer F]
? F
: T extends [infer F, ...infer R]
? F extends string
? `${F}.${Join<Extract<R, string[]>>}`
: never
: string
let value: Join<PathsToStringProps<TranslationType> // "key1.${string}" | "sub.key2.${string}"
The current output is
"key1.${string}" | "sub.key2.${string}"
but I am aiming for "key1" | "sub.key2"
.