Sub topic 4
Mixing Known Properties with an Index Signature
This chapter assumes you know basic TypeScript types, interfaces, type aliases, union types, and undefined. It also assumes you know JavaScript object property access with dot notation and bracket notation.
An index signature describes properties whose names are not known when you write the type. You can also declare specific, known properties in the same type. The important rule is that every known property must be assignable to the index signature's value type.
The compatibility rule
Suppose an object stores numeric scores under arbitrary names:
interface Scores {
[name: string]: number;
alice: number;
bob: number;
}
const scores: Scores = {
alice: 90,
bob: 85,
carol: 92,
};The string index signature says that any string key has a number value. The named properties alice and bob satisfy that rule.
The compiler checks named properties even when they are not written in the object literal:
interface InvalidScores {
[name: string]: number;
comment: string;
// Error: Property 'comment' of type 'string'
// is not assignable to 'string' index type 'number'.
}comment conflicts with the index signature because a caller can access it through a string key:
declare const scores: Scores;
const key: string = "alice";
const value = scores[key]; // numberIf a known property could contain a string, the type of scores[key] would not safely be only number.
flowchart TD
A["String index signature"] --> B["Every string key has the declared value type"]
B --> C["Known properties are checked against that type"]
C --> D["Compatible: count is number"]
C --> E["Incompatible: label is string"]Widening the value type
If known properties genuinely have different types, include all those types in the index signature:
interface Config {
[key: string]: string | number;
name: string;
retries: number;
}
const config: Config = {
name: "worker",
retries: 3,
environment: "production",
};Now every property accessed with a string key has type string | number:
const key: string = "retries";
const value = config[key];
if (typeof value === "number") {
console.log(value.toFixed(0));
} else {
console.log(value.toUpperCase());
}A union type is safe because the caller must handle each possible member. Do not use a broad type such as any just to silence an incompatibility. That removes useful checking for every key.
| Design | Meaning | Column |
|---|---|---|
| `[key: string]: number` | Every string key maps to a number | |
| `[key: string]: string \ | number` | Every string key maps to either a string or a number |
| `[key: string]: unknown` | Values vary, and callers must narrow them before use |
Separating metadata from dictionary entries
A common mistake is placing unrelated metadata beside dictionary entries:
interface BadInventory {
[product: string]: number;
updatedAt: string; // Error
}The compiler rejects this because updatedAt is also a possible string-keyed entry. Use a nested property when the metadata has a different shape:
interface Inventory {
updatedAt: string;
quantities: {
[product: string]: number;
};
}
const inventory: Inventory = {
updatedAt: "2026-11-25",
quantities: {
apples: 12,
oranges: 8,
},
};This design makes the boundary clear. updatedAt is metadata, while quantities is the dictionary. It also prevents a product lookup from accidentally returning a date string.
> callout note > If a known property does not represent a dictionary entry, move the dictionary into a nested property instead of widening the index signature unnecessarily.
Optional and readonly known properties
A known property can be optional if its type is still compatible with the index signature. With strictNullChecks, include undefined when necessary:
interface Settings {
[key: string]: string | undefined;
theme?: string;
language: string;
}A readonly named property prevents assignment through that name, but it does not make all dictionary entries readonly:
interface Labels {
readonly title: string;
[key: string]: string;
}
const labels: Labels = {
title: "Home",
subtitle: "Welcome",
};
labels.subtitle = "Hello";
// labels.title = "Dashboard"; // Error: read-only propertyUse a readonly index signature when every entry must be read-only:
interface ReadonlyLabels {
readonly [key: string]: string;
readonly title: string;
}Exercise
Why does updatedAt: string conflict with [key: string]: number, and what design avoids the conflict?
Show answer
A string index can refer to updatedAt, so its value must be a number. Put the dictionary in a nested property such as quantities and keep updatedAt beside it.