The Record
type in TypeScript is used to create a dictionary of key-value pairs, where the keys and values can have specific types. A Record
type is essentially an object type, but it provides a way to specify the types of the keys and values for better type checking and code readability.
The basic syntax for creating a Record
type is:
let myRecord: Record<KeyType, ValueType>;
Where KeyType
and ValueType
are the types of the keys and values, respectively.
For example, consider a scenario where you want to create an object that represents a score board in a game of rock-paper-scissors. You can define a Record type with the keys being the possible moves (e.g. "A X" for opponent's move being Rock and user's move being Scissors) and the values being the scores for each combination of moves.
const scoreBoard: Record<"A X" | "A Y" | "A Z" | "B X" | "B Y" | "B Z" | "C X" | "C Y" | "C Z", number> = {
"A X": 4,
"A Y": 8,
"A Z": 3,
"B X": 1,
"B Y": 5,
"B Z": 9,
"C X": 7,
"C Y": 2,
"C Z": 6,
};
In this example, the keys of the scoreBoard object are of the type "A X" | "A Y" | "A Z" | "B X" | "B Y" | "B Z" | "C X" | "C Y" | "C Z"
, which represents the possible combinations of moves in the game. The values of the object are of the type number
.
With the Record
type, you can enforce that only valid combinations of moves are used as keys in the object. If you try to access a key that doesn't exist in the object, you'll get a type error, helping you catch errors early in the development process.
const score = scoreBoard["A Z"]; // score is of type number
const invalidScore = scoreBoard["D X"]; // Type 'undefined' is not assignable to type 'number'
Additionally, the Record
type provides better code completion and documentation in your IDE
, making it easier to understand the expected keys and values in your objects
.
You can also use a Record
as a type for a function's parameter or return type:
function myFunction(data: Record<string, number>): Record<string, number> {
// do something with data
return data;
}
Another use case for Record
is when you want to create an object with a fixed set of keys and specific types for the values. This can be useful when working with configuration objects or data that should only have a specific set of keys and values.
Using the Record
type in your code provides several benefits, including:
Improved code readability: The
Record
type makes it clear what types of keys and values are expected in the object, making it easier for other developers to understand your code.Better type checking: TypeScript will catch errors if you try to assign a value of the wrong type to a key in the object.
Consistency:
The Record type enforces consistency by ensuring that all keys and values in the object have the same types, reducing the likelihood of bugs caused by inconsistent data.
In conclusion, the Record
type in TypeScript is a powerful tool for defining objects with specific types for keys and values. By using the Record
type, you can write more readable, type-safe code that is less prone to bugs caused by inconsistent data.
Top comments (0)