Proposal: Dictionary
s97712 opened this issue · comments
WuWen commented
Define
interface Dictionary<T> {
[key: string]: T;
}
Usage
let v1: Dictionary<number>;
let v2: Dictionary<string>;
let v3: Dictionary<any>;
This is a very common and useful definition, also mentioned in the official documentation.
Homa Wong commented
IMO that is not much difference than Record
.
let v1: Record<string, number>;
let v2: Record<string, string>;
let v3: Record<string, any>;
Fabio Spampinato commented
I agree with @unional, Record
should be used for this.
What if I want a dictionary where keys are numbers? In theory all object keys will be converted to strings in JS anyway, but TS at the type system level allows for objects having numeric indexes.
Sindre Sorhus commented
I'm mildly against this for two reasons:
- You only save a few characters from
Record
which is more flexible and more well known. - You shouldn't use an object as a dictionary. We have
Map
in JS now.
Dimitri B. commented
Same here, Record
should be used for this.
Sindre Sorhus commented
I'm declining this.