Is there a way to use ts-toolbelt's Object.Paths and String.Join to generate all paths of an object as String literals with dot separators, creating a Union type like the example below?
// Object
const obj = {
a: 'abc',
b: 'def',
c: {
c1: 'c1',
100: 'c2',
}
}
// Type
type dottedType = 'a' | 'b' | 'c.c1' | 'c.100'
I came across this solution that doesn't involve using ts-toolbelt:
type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...0[]]
type Join < K, P > = K extends string | number ?
P extends string | number ?
`${K}${"" extends P ? "" : "."}${P}` :
never :
never
type Leaves < T, D extends number = 10 > = [D] extends[never] ?
never :
T extends object ?
{
[K in keyof T] - ? : Join < K,
Leaves < T[K],
Prev[D] >>
}[keyof T] :
""
// Object
const obj = {
a: 'abc',
b: 'def',
c: {
c1: 'c1',
100: 'c2',
}
}
// Type
type dottedType = Leaves < typeof obj >
// type dottedType = 'a' | 'b' | 'c.c1' | 'c.100'
Is there a simpler way to achieve this with ts-toolbelt?