Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript native shorthand for value dictionary

Tags:

typescript

I just create a dictionary Interface like:

export default interface IDictionary<T> {
  [key: string]: T;
}

where I'm able to declare variables like

const myDic1 = IDictionary<boolean>;
const myDic2 = IDictionary<number>;

But I would like to know if is there any other (more universal) notation in typescript that describe the same or similiar type of interface.

Or should I import my IDictionary in every project of mine.

like image 462
Daniel Santos Avatar asked Dec 08 '25 10:12

Daniel Santos


1 Answers

The predefined type Record can be used to obtain an equivalent type.

declare const myDic1: Record<string, boolean>;
declare const myDic2: Record<string, number>;

Record is usually used for more specific keys (ex: Record<"key1" | "key2", boolean>) but it works with string as well.

like image 142
Titian Cernicova-Dragomir Avatar answered Dec 10 '25 10:12

Titian Cernicova-Dragomir