typescript / intermediate
Snippet
Index Signatures for Dynamic Keys
Index signatures allow objects to have flexible keys that aren't known ahead of time. You define the type of the key (usually string or number) and the type of the value it returns.
snippet.ts
1
2
3
4
5
6
7
8
9
10
11
12
interface StringConfig {[key: string]: string | number;id: string;version: number;}const config: StringConfig = {id: "A1",version: 1.2,theme: "dark",retries: 5};
Breakdown
1
[key: string]: string | number;
Specifies that any string key can be used as long as the value is a string or a number.