Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Dictionary #33

Closed
s97712 opened this issue Apr 27, 2019 · 5 comments
Closed

Proposal: Dictionary #33

s97712 opened this issue Apr 27, 2019 · 5 comments

Comments

@s97712
Copy link

s97712 commented Apr 27, 2019

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.

@unional
Copy link

unional commented Jun 10, 2019

IMO that is not much difference than Record.

let v1: Record<string, number>;
let v2: Record<string, string>;
let v3: Record<string, any>;

@fabiospampinato
Copy link
Contributor

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.

@sindresorhus
Copy link
Owner

I'm mildly against this for two reasons:

  1. You only save a few characters from Record which is more flexible and more well known.
  2. You shouldn't use an object as a dictionary. We have Map in JS now.

@BendingBender
Copy link
Contributor

Same here, Record should be used for this.

@sindresorhus
Copy link
Owner

I'm declining this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants