Create a dictionary object with Typescript interface
1 min read
Prequisite:
JavascriptTypescript
- Should understand Typescript type
- Basic understanding of Javascript
When using Typescript, you sometimes have to define the type of value for any variables. Most of the time, Typescript is smart enough to know what the type is, but there are just a few cases where it cannot define the type for you and you are obligated to define the type. This is especially true of dictionary objects. Most people starting off with Typescript will forget you need to define two types to create a dictionary object. Particularly if the dictionary property names are dynamic names such as id numbers. Anyway, here is a simple type of example so you can understand what needs to be done.
// Define the object typeinterface object {name: string | null;age: number | null;};
// Define the dictionary object type interface dictionary {[idNumber: number]: object;};