Sometimes, we want to create an object index key type in TypeScript.
In this article, we’ll look at how to create an object index key type in TypeScript.
How to create an object index key type in TypeScript?
To create an object index key type in TypeScript, we can use index signatures.
For instance, we write
interface IDictionary<TValue> {
[id: string]: TValue;
}
class Test {
private dictionary: IDictionary<string>;
//...
}
to create the IDictionary
interface that takes the TValue
generic type.
Then we can use it as the type for the dictionary
variable in the Test
class.
As a result, dictionary
should be assigned an object with all string property values.
Conclusion
To create an object index key type in TypeScript, we can use index signatures.