Sub topic 2
Declaring an Index Signature
Prerequisites and the dictionary problem
This chapter assumes that you know basic TypeScript types, interfaces, type aliases, union types, and the undefined type. You should also know JavaScript property access with dot notation and bracket notation.
A dictionary object stores values under keys that are not all known when you write the program. For example, a scoring application might receive player names from a server. You know each score is a number, but you do not know every player name in advance.
Without an index signature, TypeScript cannot describe this relationship clearly:
const scores = {
alice: 90,
bob: 85,
};An index signature says, “Any property with this kind of key has this value type.”
Basic index signature syntax
The syntax is:
interface ScoreTable {
[key: string]: number;
}The name key is a local name used only in the type declaration. It does not create a property named key. The important parts are the key type, string, and the value type, number.
interface ScoreTable {
[playerName: string]: number;
}
const scores: ScoreTable = {};
scores["alice"] = 90;
scores.bob = 85;
console.log(scores["alice"]); // 90
console.log(scores.bob); // 85
// scores.carol = "A"; // Error: string is not assignable to numberBoth bracket notation and dot notation work for string-named properties. The compiler checks that every assigned value is a number.
You can write the same type as a type alias:
type ScoreTable = {
[playerName: string]: number;
};Interfaces and type aliases both support index signatures. Choose the form that matches the style of the surrounding code.
Which key types are allowed
An index signature can use string, number, or symbol as its key type.
| Key type | Example access | Typical use |
|---|---|---|
| `string` | `scores["alice"]` | Names, IDs, and general property names |
| `number` | `rows[0]` | Numeric collections and array-like objects |
| `symbol` | `metadata[token]` | Non-string keys that must avoid name collisions |
Here are the three forms:
interface StringDictionary {
[key: string]: number;
}
interface NumberDictionary {
[index: number]: string;
}
interface SymbolDictionary {
[key: symbol]: string;
}
const rows: NumberDictionary = {
0: "first",
1: "second",
};
const token = Symbol("token");
const metadata: SymbolDictionary = {
[token]: "private value",
};
console.log(rows[0]);
console.log(metadata[token]);JavaScript converts ordinary object property names to strings, while symbols remain separate property keys. A number index signature describes numeric access, such as rows[0]. It does not make the object a true array, so array methods such as map are not provided automatically.
A type can contain both string and number index signatures, but the number-indexed value type must be compatible with the string-indexed value type:
interface MixedDictionary {
[key: string]: string | number;
[index: number]: string;
}
const values: MixedDictionary = {
0: "zero",
name: "example",
count: 3,
};Numeric property access can also be represented as a string property at runtime, so TypeScript applies this compatibility rule.
Known properties and index signatures
A string index signature also constrains explicitly named properties in the same type. Every named property must be assignable to the index signature's value type.
interface Config {
[key: string]: string | number;
name: string;
retries: number;
}
const config: Config = {
name: "worker",
retries: 3,
region: "us-east-1",
};This version is invalid because retries is a number but the index signature allows only strings:
// interface BrokenConfig {
// [key: string]: string;
// retries: number;
// }Use a union when known properties and dynamic properties genuinely have different value types. If they should not be mixed, separate the dynamic dictionary into its own property instead.
Exercise
What value type is required by this declaration: interface Flags { [name: string]: boolean }?
Show answer
Every string-named property accessed through Flags must contain a boolean value.