What is index signature in TypeScript?

Index signatures in TypeScript allow us to define the shape of objects and how they can be accessed using an index. Unlike traditional object properties, which are explicitly named, index signatures provide a more dynamic way to access and define properties.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Syntax

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Here, [index: string] indicates that the object can be accessed using a string index, and valueType specifies the type of the corresponding property.

Code example

Let’s consider a scenario where we want to create a data structure to store information about various books. Some books may have additional properties beyond a predefined set. Here's how we can use index signatures to achieve this functionality:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Code explanation

Let's break down the above example line by line:

  • Interface declaration

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

We declare an interface Book with three mandatory properties (title, author, and year). The index signature [key: string]: string | number allows any additional properties with string or number values.

  • Book objects

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

We create two book objects (book1 and book2) based on the Book interface. The objects include the mandatory properties and additional properties (genre and language) allowed by the index signature.

  • Output

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

We print the book objects to the console to observe the structure and properties.

Applications and usage

Following are some of the applications and uses of index signatures in TypeScript:

  • Dynamic data structures: Index signatures are commonly used when dealing with data structures where the shape of objects may vary, such as configurations, user inputs, or API responses. They provide a mechanism to handle dynamic properties without sacrificing type safety.

  • Extensible interfaces: Index signatures enable interfaces to define a core set of properties while allowing for the addition of arbitrary properties. This is particularly useful in scenarios where objects may have optional or customizable attributes.

  • Serialization and deserialization: Index signatures can facilitate the serialization and deserialization of data by accommodating additional properties during the conversion process. This allows for seamless transformation between TypeScript objects and JSON representations.

  • Configuration objects: In applications that rely on configuration objects to customize behavior, index signatures can be employed to capture a wide range of configuration options without the need for exhaustive predefined properties.

  • Data transformation pipelines: Index signatures can play a role in data transformation pipelines where input data may have varying structures. They provide a flexible mechanism to handle diverse data formats and adapt to changing requirements during processing.

Conclusion

Index signatures in TypeScript offer a dynamic way to define the shape of objects and allow for flexibility in accessing and defining properties. By using index signatures, developers can create more adaptable data structures that accommodate additional properties beyond a predefined set. This enhances the versatility and extensibility of TypeScript codebases, enabling the handling of diverse data structures with ease.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved