HomeToolsAbout

Object Strategies

Typing Object

Source

// dictionary = any number of properties of the same type interface StringDictionary { [key: string]: string; } const obj: StringDictionary = { key1: "value-1", key2: "value-2", keyn: "value-n" }; // record = equivalent to dictionary above const obj: Record<string, string> = { key1: "value-1", key2: "value-2", keyn: "value-n", }; // enums enum KeysEnum { FIRST = "firstKey", SECOND = "secondKey", } type EnumDictionary = Record<KeysEnum, string>; const obj: EnumDictionary = { [KeysEnum.FIRST]: "value1", [KeysEnum.SECOND]: "value2", };
AboutContact