Without explicit declaration of key
as index
, the key
passed to an object via object[key]
will result in type error
const allTypes = { "thisthing": "", "anotherthing": "", "something": "find this", }; const lookUpKey = "something"; // must delcare as keys of a type const valueToLookFor = allTypes[lookUpKey as keyof typeof allTypes]
interface MixedDictionary { // static staticKey: string; // dynamic = dictionary [key: string]: string | number; } const obj: MixedDictionary = { staticKey: "staticValue", dynamicKey1: "value-1", dynamicKey2: "value-2", dynamicKeyn: "value-n", };