sindresorhus / type-fest

A collection of essential TypeScript types

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proposal: Dictionary

s97712 opened this issue · comments

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.

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:

  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.

Same here, Record should be used for this.

I'm declining this.