Typescript

How to Initialize and Declare Dictionary in TypeScript

A “dictionary” refers to a type of data structure that contains key-value pairs in an unordered list. It is equivalent to “map” in TypeScript. It is a useful tool for handling data in TypeScript applications. The process of declaring and initializing a dictionary in TypeScript is simple and easy. The dictionary is commonly declared and initialized in TypeScript using the “Record” utility type.

This post will describe the methods for declaring and initializing a dictionary in Typescript.

How to Initialize and Declare a Dictionary in TypeScript?

To initialize and declare a dictionary in TypeScript, use the following approaches:

Before moving ahead, first understand that for executing a TypeScript file, it must be transpile into a JavaScript file after every modification and then run the JavaScript code on the terminal using the given commands:

tsc dictionary.ts
node dictionary.js

 

Method 1: Initialize and Declare Dictionary in TypeScript Using an Indexed Object

To initialize and declare a dictionary, use the “indexed object”. It is a data structure that links a group of keys to a group of values, where each key is distinct and links to a particular value.

Syntax

The following syntax is utilized for initializing and declaring a dictionary using the indexed object:

type MyDictionary = {
 [key: type]: valueType
};

 
Example

First, we will define a dictionary named “MyDictionary” which is an indexed object type that describes the shape of our dictionary:

type MyDictionary = {
 [key: string]: number
};

 
Then, we declare and initialize a new variable “ageDictionary” of type “MyDictionary” and assign an object literal to it, with three key-value pairs:

const ageDictionary: MyDictionary = {
 "John": 26,
 "Mary": 28,
 "Rock": 27
};

 
Print the dictionary on the console by passing the variable in the “console.log()” method:

console.log(ageDictionary);

 
Output


If you want to retrieve the value of any specified key of a dictionary then, you can use the square bracket notation “[ ]”:

console.log(ageDictionary["Rock"]);

 
The output displays the age of “Rock” that is “27”:

Method 2: Initialize and Declare Dictionary in TypeScript Using an Interface

For initializing and declaring a dictionary, you can use the “interface”. An interface in TypeScript is a technique for expressing a contract that an object must follow. It defines the attributes and types of properties that an object requires in order to be considered an instance of that interface.

Syntax

Follow the given syntax for initializing and declaring a dictionary using interface:

interface Info {
 key1: value1Type;
 key2: value2Type;
}

 
Example

First, define a dictionary named “Info” using an “interface” which requires any object that implements it to have a name property of “string” type and an age property of “number” type:

interface Info {
 name: string;
 age: number;
}

 
Then, declare and initialize a new variable “studentDictionary” of type “Info” with an attribute “id” of type “number”:

var studentDictionary: { [id: number]: Info; } = {
 1: { name: "Jack", age: 15 },
 2: { name: "Linta", age: 18 }
 };

 
Lastly, print the dictionary on the console:

console.log(studentDictionary);

 
Output


Now, we will access the object at index or id 2:

 console.log(studentDictionary[2]);

 
Output

Method 3: Initialize and Declare Dictionary in TypeScript Using ES6 Map

You can also use the ES6 Map approach for initializing and declaring a dictionary in TypeScript. It is a built-in data structure in JavaScript and TypeScript that enables storing key-value pairs, where the key and the value can be of any data type.

Syntax

For using ES6 Map, follow the given syntax:

new Map<keyType, valueType>();

 
Example

First, we will create a map object using Map constructor by specifying the type of keys and values as “string”:

let student = new Map<string, string>();

 
Now, utilize the set() method to add the key-value pairs to the dictionary:

student.set("name", "Linta");
student.set("age", "18");
student.set("hobby", "Book Reading");

 
Print the dictionary on the console:

console.log(student);

 
Output

Method 4: Initialize and Declare Dictionary in TypeScript Using Record Utility Type

Another way to initialize and declare a dictionary is to use the “Record” utility type in TypeScript. It is the most common way of initializing and declaring a dictionary. It is a pre-built data structure in TypeScript that enables creating a type with specified keys and values.

Syntax

The following syntax is used for the Record utility type:

Record<keyType, valueType> = {};

 
Example

In the given example, we declare and initialize a dictionary called “student” using the “Record” utility type to specify the keys and values of type string:

let student: Record<string, string> = {
 "name": "Linta",
 "age": "18",
 "hobby": "Book Reading",
};

 
Finally, print the dictionary on the console:

console.log(student);

 
Output


We have compiled all the essential information related to the initializing and declaring a dictionary in TypeScript.

Conclusion

To initialize and declare a dictionary in TypeScript, use the “Indexed object”, “an Interface”, “ES6 Map” or the “Record utility type”. The most common way of initializing and declaring a dictionary is the “Record utility type”. This post described the methods for declaring and initializing a TypeScript dictionary.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.