Proposal: Dictionary
s97712 opened this issue · comments
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.
IMO that is not much difference than Record
.
let v1: Record<string, number>;
let v2: Record<string, string>;
let v3: Record<string, any>;
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.
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.
Same here, Record
should be used for this.
I'm declining this.