DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > How the TypeScript Record Type Works

How the TypeScript Record Type Works

Let's take a look at how the TypeScript Record type works.

Johnny Simpson user avatar by
Johnny Simpson
CORE ·
Apr. 13, 22 · Web Dev Zone · Tutorial
Like (4)
Save
Tweet
2.41K Views

Join the DZone community and get the full member experience.

Join For Free

TypeScript Records are a great way to ensure consistency when trying to implement more complex types of data. They enforce key values and allow you to create custom interfaces for the values.

That sounds confusing, but let's see how it works in practice.

Utility Types

A Record is a utility type - that means it is a type especially defined by TypeScript to help with a certain problem.

How Typescript Record Types Work

Suppose you have a data set like this:

TypeScript
 
const myData = {
    "123-123-123" : { firstName: "John", lastName: "Doe" },
    "124-124-124" : { firstName: "Sarah", lastName: "Doe" },
    "125-125-125" : { firstName: "Jane", lastName: "Smith" }
}

Our data set has an ID for its key, which is of type string. All of the values have the same format - that is, they have a firstName and lastName.

For this data structure, a Record is the best utility type to use. We can define our data structure type as follows:

TypeScript
 
type User = {
    firstName: string,
    lastName: string
}

const myData:Record<string, User> = {
    "123-123-123" : { firstName: "John", lastName: "Doe" },
    "124-124-124" : { firstName: "Sarah", lastName: "Doe" },
    "125-125-125" : { firstName: "Jane", lastName: "Smith" }
}

A Record takes the form Record<K, T>, where K is the type of the key and T is the type of the values.

Above, we defined a new type User for our values and set our keys to type string.

Record Types and Union Types

Sometimes, we can have an object with a predefined set of possible keys. This is particularly true when calling from an API. For example:

TypeScript
 
const myData = {
    "uk" : { firstName: "John", lastName: "Doe" },
    "france" : { firstName: "Sarah", lastName: "Doe" },
    "india" : { firstName: "Jane", lastName: "Smith" }
}

Let's presume that for our data set above, the key can only be three values: UK, France, or India. In this case, we can define a type for User and a union type for our key:

TypeScript
type User = {
    firstName: string,
    lastName: string
}
type Country = "UK" | "France" | "India";

const myData:Record<Country, User> = {
    "UK" : { firstName: "John", lastName: "Doe" },
    "France" : { firstName: "Sarah", lastName: "Doe" },
    "India" : { firstName: "Jane", lastName: "Smith" }
}

Using this method, we can enforce strict rules about the values the key is allowed to be, along with the type our values should conform to.

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Progressive Web Apps vs Native Apps: Differences and Similarities
  • Java’s Encapsulation - When the Getter and Setter Became Your Enemy
  • When Writing Code Isn't Enough: Citizen Development and the Developer Experience
  • Is DataOps the Future of the Modern Data Stack?

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo