How Records in Type Script Can Enhance Your Coding Experience

How Records in Type Script Can Enhance Your Coding Experience

A more flexible yet more robust object

Introduction

While solving DSA problems i came across Record. It saved me from using more memory and time at the same time meaning improving both time and space complexity.

What is Record

It is an object-like structure. An object type whose property keys are Keys and whose property values are Type. Here you can assign key and value and the respective type of your own.

Syntax

const record_name: Record<Type, Type> = {};

//Example
const numberrelationship: Record<number, number> = {};

It will initialize an empty record names numberrelationship whose keys will be of type number and values also will be of type number.

const numberrelationship: Record<number, number> = {};
numberrelationship[1] = 10;
console.log(numberrelationship);
// output will be
// {
//  "1": 10
// }

Use cases

Well, from its structure and nature it can be useful at tons of situations. The ability to assign user defined key of any type that suits a situations makes it really versatile and flexible.

Refferences

Record in typescript

More on Record