The Record Type in TypeScript: How to Create Types with Key-Value Pairs
TypeScript is a popular programming language that is widely used in web development. It is an extension of JavaScript that adds static typing and other features to the language. One of the features that make TypeScript stand out is its support for advanced types, such as the Record type.
In this post, we will explore the Record type in TypeScript and learn how to create types with key-value pairs.
The Record type is a built-in type in TypeScript that allows you to create an object type with key-value pairs. It is similar to the object type, but with a specific set of keys and values.
The syntax for defining a Record type is as follows:
type RecordType = Record<Keys, Type>;
Here, the Keys
parameter is a union of string literal types that represent the keys of the object, and the Type
parameter is the type of the values associated with each key.
For example, suppose we want to create a type that represents a user object with a name, age, and email. We can define a Record type as follows:
type User = Record<'name' | 'age' | 'email', string>;
This creates a type called User
that represents an object with three keys (name
, age
, and email
) and string values.
To create a Record type, you can use the Record
utility type provided by TypeScript. The Record
type takes two parameters: the keys and the values.
Here's an example of how to create a Record type:
type MyRecord = Record<'key1' | 'key2', number>;
This creates a type called MyRecord
that represents an object with two keys (key1
and key2
) and number values.
You can also use a type alias to define a Record type:
type MyRecord = {
key1: number;
key2: number;
};
This creates a type called MyRecord
that represents an object with two keys (key1
and key2
) and number values.
To access the values of a Record type, you can use the dot notation or bracket notation:
type MyRecord = Record<'key1' | 'key2', number>;
const record: MyRecord = {
key1: 10,
key2: 20,
};
console.log(record.key1); // 10
console.log(record['key2']); // 20
Here, we create a MyRecord
object with two keys (key1
and key2
) and assign values to them. We can then access the values using the dot notation (record.key1
) or bracket notation (record['key2']
).
The Record type is a powerful feature of TypeScript that allows you to create types with key-value pairs. It is useful when you need to define an object with a specific set of keys and values. In this post, we learned how to create Record types and access their values. We hope this post helps you in your TypeScript development journey!