2

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.

Yarin Barnes
  • 211
  • 1
  • 6
  • 1
    See [this](https://tsplay.dev/m0AAxW) and [this](https://stackoverflow.com/questions/68668055/eliminate-nevers-to-make-union-possible/68672512?noredirect=1#comment121362429_68672512) and [my article](https://catchts.com/deep-pick) – captain-yossarian from Ukraine Aug 23 '22 at 08:40
  • Please see the `Leaves` type in the answer to the linked question. For your code it looks like [this](https://tsplay.dev/mpLLam), producing the type `"a" | "b" | "c.d" | "c.e" | "c.f.g" | "c.f.h.i"`. – jcalz Aug 23 '22 at 15:17

0 Answers0