1

I want to construct a union type from object keys and nested objects keys.

I have object

type NestedObject = {
  prop1: {
    nestedProp1: string;
    nestedProp2: number;
  };
  prop2: {
    nestedProp3: boolean;
    nestedProp4: symbol;
  };
  prop3: string;
};

I want union that that looks like

type Type =
  | 'prop1.nestedProp1'
  | 'prop1.nestedProp2'
  | 'prop2.nestedProp3'
  | 'prop2.nestedProp4'
  | 'prop3';
Marius
  • 1,664
  • 2
  • 16
  • 28

1 Answers1

0

Found this article and this will help me https://dev.to/pffigueiredo/typescript-utility-keyof-nested-object-2pa3

type NestedKeyOf1<ObjectType extends object> = {
  [Key in keyof ObjectType & (string | number)]: ObjectType[Key] extends object
    ? `${Key}` | `${Key}.${NestedKeyOf<ObjectType[Key]>}`
    : `${Key}`;
}[keyof ObjectType & (string | number)];
Marius
  • 1,664
  • 2
  • 16
  • 28