How I can generate all paths of an object as String literal's with dot separator with ts-toolbelt's Object.Paths
and String.Join
as Union type like this?
// Object
const obj = {
a: 'abc',
b: 'def',
c: {
c1: 'c1',
100: 'c2',
}
}
// Type
type dottedType = 'a' | 'b' | 'c.c1' | 'c.100'
I found this solution without 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'
How can I simplify this code with ts-toolbelt?