TS1337

An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead.

Broken Code ❌

export interface StreetMap {
  [city: 'New York']: ['Broadway', 'Park Avenue'];
}

Fixed Code ✔️

export type StreetMap = {
  'New York': ['Broadway', 'Park Avenue'];
};

Alternative:

export interface StreetMap {
  [city: string]: ['Broadway', 'Park Avenue'];
}

An index signature parameter type cannot be a union type. Consider using a mapped object type instead.

Broken Code ❌

interface CustomError {
  [key: string | number]: string | number;
}
 
const error: CustomError = {
  401: 'Unauthorized',
};

Fixed Code ✔️

Solution with mapped object type:

type CustomError = {
  [key in number | string]: string | number;
};
 
const error: CustomError = {
  401: 'Unauthorized',
};

Alternative:

interface ErrorNumber {
  [key: number]: string | number;
}
 
interface ErrorString {
  [key: string]: string | number;
}
 
const error: ErrorNumber | ErrorString = {
  401: 'Unauthorized',
};