Typescript recursive type of object to string

55 Views Asked by At

Lets say i have this object (it came from a json)

    const obj = {
      a: '1',
      b: '1',
      c: {
        d: '2',
        e: '2',
        f: {
          g: '3',
          h: {
            i: '4',
          },
        },
      },
    };

i want to declare a type of string that will autocomplete the nested object.
i managed to accomplish something like that but it is far from perfect:

type RecuresiveKeyOf<TObj> = {
  [TKey in keyof TObj & string]: {
    [TKey1 in keyof TObj[TKey] & string]: `${TKey}.${TKey1}`;
  }[keyof TObj[TKey] & string];
}[keyof TObj & string];

now, when i try to use it i get this autocomplete.
but i also get unnecessary fields of string

for conclusion i want it to be able to auto complete for me the nested object as a string.
note that the amount of levels of the nested object is unknown.

0

There are 0 best solutions below