Typescript Dictionary Complete Guide with examples

Typescript dictionary class: Learn with examples.

In this Blog Post, Learn the dictionary in typescript with examples.

Dictionary is a type of data structure with unordered list data, that contains key and value values. It is the same as map type in typescript.

Does TypeScript have dictionary?

Typesript has no dictionary name type, however it has Map that stores list of key and values. And data is unordered key and values. Es6 introduced Map type and created Map object using types in Typescript. You can create a dictionary using map as given below Using new and Map keyword with key and value types are declared. valid key and value types are string,number,boolean and class or interface objects.

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

How to declare and initialize a dictionary in a Typescript?

dictionary can be created using map keyword in typescript.

A map is a type introduced in ES6, It adds type safety to key and values in typescript.

  • the declare and create a map variable using new operator and key and value types are declared

    let employees = new Map<string, string>();
  • add the key-value pairs to an map using set method.

    employees.set("name", "john");
  • check for key exists or using has() method

// Checking for the key exist :
employees.has("name"); // true
  • get the value with a key using get method.
// get a value by a key:
employees.get("name"); // john
  • delete a key and its value using delete method.
// delete an item by a key:
employees.delete("name");
  • Iterate the map with keys and values using forEach with callback
// iterate key and values
employees.forEach((item, key) => console.log(item));
  • Remove all elements(keys and value) from a map
// remove all from map:
employees.clear();
  • get the number of elements in a map
// size:Number of elements in Map :
console.log(employees.size);
  • retrieve all keys and values with insertion order
// get All keys with insertion order
let keys = Array.from(employees.keys());

// Extract values with insertion order
let values = Array.from(employees.values());

Here is an complete example

let employees = new Map<string, string>();
employees.set("name", "john");

// Checking for the key exist :
employees.has("name"); // true

// get a value by a key:
employees.get("name"); // john

// delete an item by a key:
employees.delete("name");
// iterate key and values
employees.forEach((item, key) => console.log(item));

// remove all from map:
employees.clear();

// size:Number of elements in Map :
console.log(employees.size);

// get All keys with insertion order
let keys = Array.from(employees.keys());

// Extract values with insertion order
let values = Array.from(employees.values());

How to get key values from a dictionary in TypeScript?

following are steps required to get key values from a dictionary or map in typescript.

  • one way using map.keys returns list of keys
  • Another using for each
employees.forEach((item, key) => console.log(key));

How to iterate through keys in TypeScript Dictionary?

Multiple ways to return the keys of an map in Typescript One way using

How to declare an interface for a dictionary in typescript?

Declare an interface using indexable type in typescript

declare an interface that accepts a key as a string and value of type T

interface Dictionary<T> {
  [Key: string]: T;
}

A class contains properties of the dictionary interface as below.

export class AllEmployees {
  employees: Dictionary<string> = {};
}

Dictionary interfaces declare with let keyword and initialize the values, add the values.

let emps: Dictionary<string> = {};
emps["name"] = "john";

How to declare a dictionary with record type in typescript

Record is a map of key and value pairs.

A new type creates with a type alias(type keyword) and Record type.

type emps = Record<string, string>;

Data initialized using object syntax as seen below.

let emps: Record<string, string> = {
   {"name": "john" },
   {"id":  "1" }
};

Conclusion

Learned typescript dictionaries with multiple examples to declare a dictionary type in typescript