Record<Keys,Type>
Construct an object type whose property key is Keys and property value is Type. Used to map properties of one type to another.
Simply put, Record in TypeScript can implement and define the key and value types of an object, and the generics behind Record are the types of object keys and values.
example
For example, if I need a cats object, there are three different properties in this object, and the values of the properties must be numbers and strings
, so I can write it like this:
interface CatInfo {
age: number;
breed: string;
}
type CatName = "mincat" | "licat" | "mordred";
const cats: Record<CatName, CatInfo> = {
mincat: {
age: 10, breed: "小猫er" },
licat: {
age: 5, breed: "李猫er" },
mordred: {
age: 16, breed: "无名猫er" },
};
cats.licat;
const cats: Record<CatName, CatInfo>
Learning documentation: https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeystype