typescripttypestypescript-typings

How in typescript declare an interface key as specific type?


I have the interface:

export type TCategoryAnalyzes = {
  categoryId: string,
  analyzes: IAnalyzesStorageData[],
  region: string,
};

The goal is to make it something like:

export type TCategoryAnalyzes = {
  region: { // string
    categoryId: string,
    analyzes: IAnalyzesStorageData[],
  }
};

Where the region key must be of a string type


Solution

  • export type TCategoryAnalyzes = {
      [region: string]: {
        categoryId: string;
        analyzes: IAnalyzesStorageData[];
      };
    };
    

    this his how you can achieve it.