All you need is to define the index signature:
const allTypes: {[key: string]: boolean} = { jpg: true, gif: true, png: true, mp4: true };
Indexable Types
Similarly to how we can use interfaces to describe function types, we can also describe types that we can “index into” like
a[10], orageMap["daniel"]. Indexable types have an index signature that describes the types we can use to index into the object, along with the corresponding return types when indexing. Let’s take an example:interface StringArray { [index: number]: string; } let myArray: StringArray; myArray = ["Bob", "Fred"]; let myStr: string = myArray[0];Above, we have a
StringArrayinterface that has an index signature. This index signature states that when aStringArrayis indexed with anumber, it will return astring.
Utility type: Record<Keys, Type>
Another solution is to use the TypeScript utility type Record<Keys, Type>:
Constructs an object type whose property keys are
Keysand whose property values areType. This utility can be used to map the properties of a type to another type.
const allTypes: Record<string, boolean> = { jpg: true, gif: true, png: true, mp4: true };
for (const key of Object.keys(allTypes)) {
console.log(`${key}: ${allTypes[key]}`);
}