TS1337
error TS1337: An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead.
Broken Code ❌
1 2 3
| export interface StreetMap { [city: 'New York']: ['Broadway', 'Park Avenue']; }
|
Fixed Code ✔️
1 2 3
| export type StreetMap = { 'New York': ['Broadway', 'Park Avenue']; };
|
Alternative:
1 2 3
| export interface StreetMap { [city: string]: ['Broadway', 'Park Avenue']; }
|
error TS1337: An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
Broken Code ❌
1 2 3 4 5 6 7
| interface CustomError { [key: string | number]: string | number; }
const error: CustomError = { 401: 'Unauthorized', };
|
Fixed Code ✔️
Solution with mapped object type:
1 2 3 4 5 6 7
| type CustomError = { [key in number | string]: string | number; };
const error: CustomError = { 401: 'Unauthorized', };
|
Alternative:
1 2 3 4 5 6 7 8 9 10 11
| interface ErrorNumber { [key: number]: string | number; }
interface ErrorString { [key: string]: string | number; }
const error: ErrorNumber | ErrorString = { 401: 'Unauthorized', };
|