HomeAbout

Object

Typing Object with unknown number of records with consistent type

// 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", };

Typing object with limited/restricted key definitions

// enums = predefined, but can grow dynamically via the definition enum KeysEnum { FIRST = "firstKey", SECOND = "secondKey", } type EnumDictionary = Record<KeysEnum, string>; const obj: EnumDictionary = { [KeysEnum.FIRST]: "value1", [KeysEnum.SECOND]: "value2", };

Optional Field

This is to make the variable an optional type.

export interface ISearchResult { title: string; listTitle:string; entityName?: string, lookupName?:string, lookupId?:string }

Otherwise, declared variables shows undefined if this variable is not used.

{ entityName?: string; } // equivalent to: { entityName: string | undefined; }
AboutContact