Sergey Tyupaev
Sergey Tyupaev

Reputation: 1273

How to define nested dictionary type in typescript

I'm struggling to define a type for an object with a nested structure like this:

const nestedDictionary = {
   a: {
     b: true
   },
   c: true,
   d: {
     e: {
       f: true
     }
   }
}

Upvotes: 1

Views: 3791

Answers (1)

Agustin Garcia
Agustin Garcia

Reputation: 139

type Dictionary = {
  [x: string]: boolean | Dictionary;
};

const nestedDictionary: Dictionary;

Or, if you prefer to use type as a parameter:

type GenericDictionary<T> = {
  [x: string]: T | GenericDictionary<T>;
};

const nestedDictionary: GenericDictionary<boolean>;

Upvotes: 8

Related Questions