Sub topic 11
How `keyof` Treats Index Signatures
Assume you know TypeScript interfaces, type aliases, union types, JavaScript objects, and basic generics. This chapter focuses on one edge case: how keyof behaves when a type allows properties that are not listed by name.
What an index signature means
An index signature describes an object whose property names follow a rule. The following type allows any string property name, as long as its value is a boolean:
type FeatureFlags = {
[key: string]: boolean;
};
const flags: FeatureFlags = {
darkMode: true,
betaSearch: false,
};
console.log(flags.darkMode);The name key is only a label for the key parameter. It does not create a property named key. The important parts are string and boolean:
- Every string key is allowed.
- Every value must be a boolean.
keyof asks which keys can be used to access a type. For an ordinary object, it produces a union of the listed property names:
type User = {
id: number;
name: string;
};
type UserKey = keyof User; // "id" | "name"With a string index signature, the answer is broader:
type FeatureFlags = {
[key: string]: boolean;
};
type FlagKey = keyof FeatureFlags; // string | numberWhy `keyof` includes `number`
JavaScript converts numeric property access into string property names. These two expressions access the same property:
const scores = {
7: 100,
};
console.log(scores[7]);
console.log(scores["7"]);Because a string index signature can also be accessed with a number, TypeScript reports string | number for keyof.
type FeatureFlags = {
[key: string]: boolean;
};
type FlagKey = keyof FeatureFlags;
const textKey: FlagKey = "darkMode";
const numericKey: FlagKey = 7;
console.log(textKey, numericKey);This does not mean that every possible number is stored in the object. It means that a number is a valid indexing operation according to the type.
String and number index signatures
A number index signature is narrower. It describes numeric indexes and produces number with keyof:
type Scores = {
[position: number]: string;
};
type ScoreKey = keyof Scores; // number
const labels: Scores = {
1: "first",
2: "second",
};
const position: ScoreKey = 1;
console.log(labels[position]);The difference is useful when designing APIs. Use a number index signature for array-like structures where numeric positions are meaningful. Use a string index signature for dictionaries where arbitrary names are meaningful.
| Type declaration | Result of `keyof` | Typical use | Column |
|---|---|---|---|
| `{ [key: string]: boolean }` | `string \ | number` | Named dictionary |
| `{ [index: number]: string }` | `number` | Array-like collection | |
| `{ online: boolean; offline: boolean }` | `"online" \ | "offline"` | Fixed property set |
Named properties must match the index signature
An object can have both an index signature and explicitly named properties. Every named property must be compatible with the index signature's value type:
type Settings = {
[key: string]: string;
theme: string;
language: string;
};
const settings: Settings = {
theme: "dark",
language: "en",
timezone: "UTC",
};
console.log(settings.theme);This definition is invalid because retries is not a string:
type InvalidSettings = {
[key: string]: string;
retries: number; // Error
};The index signature promises that any string key returns a string. Allowing retries to return a number would break that promise.
Choosing between an index signature and `Record`
Record is a utility type that often expresses the same idea:
type FeatureFlags = Record<string, boolean>;
const flags: FeatureFlags = {
darkMode: true,
betaSearch: false,
};
console.log(flags.betaSearch);Both forms allow arbitrary string keys and produce keyof FeatureFlags as string | number. Use whichever makes the intent clearer. An index signature is direct when describing the structure itself. Record is convenient when constructing a type from other type expressions.
Exercise
What is the type of keyof { [index: number]: Date }, and why does it not include string?
Show answer
It is number. The type only promises access through numeric indexes, so arbitrary string keys are not part of its key type.