Skip to main content

Typescript Types

Mapped Types​

In TypeScript, you can define an object type where the keys are strings and the values are also strings using an index signature:

interface StringObject {
[key: string]: string;
}

const myObject: StringObject = {
name: "Mariappan",
city: "Tamilnadu"
};

Explanation:

  • [key: string]: string;: This is an index signature. It defines that any key within the object must be a string, and the corresponding value associated with that key must also be a string.
  • StringObject: This is an interface that acts as a blueprint for the object you want to create.
  • myObject: This is an object that conforms to the StringObject interface.

Wiki​

  1. Exploring [key:string]: any in TypeScript
  2. Mapped Types - official
  3. Index Signatures
  4. Enforcing the type of the indexed members of a Typescript object? - stackoverflow
  5. TypeScript Function Types: A Beginner's Guide