Home > Enterprise >  What is this syntax called?
What is this syntax called?

Time:10-27

I am trying to figure out what the piece of code below means/does, and what this syntax is called in Typescript parlance? What does [name: string]: string mean?

export interface APIGatewayProxyEventPathParameters {
  [name: string]: string | undefined;
}

Edit Does it mean whatever type implements this interface needs to provide something like an array? or a map? e.g: typeA["str1"] = "str2".

CodePudding user response:

It's called an index signature.

TS Playground link

interface APIGatewayProxyEventPathParameters {
  [name: string]: string | undefined;
}

declare const params: APIGatewayProxyEventPathParameters;

params.one; // string | undefined
params['two']; // string | undefined
params[Symbol.asyncIterator]; // Error: Type 'unique symbol' cannot be used as an index type.(2538)
  • Related