# Typescript
# 高级类型Record
参考TS官方文档 (opens new window)给的定义
Record<Keys, Type> Constructs an object type whose property keys are Keys and whose property values are Type. This utility can be used to map the properties of a type to another type. 翻译: 构造一个对象类型,其属性名为Keys, 其属性值为Type。该实用程序可用于将一种类型的属性映射到另一种类型。
通俗一点,Record可以定义为: Record<K,T>构造具有给定类型T的一组属性K的类型。在将一个类型的属性映射到另一个类型的属性时,Record非常方便。 他会将一个类型的所有属性值都映射到另一个类型上并创造一个新的类型.
Example:
interface EmployeeType {
id: number
fullname: string
role: string
}
let employees: Record<number, EmployeeType> = {
0: { id: 1, fullname: "John Doe", role: "Designer" },
1: { id: 2, fullname: "Ibrahima Fall", role: "Developer" },
2: { id: 3, fullname: "Sara Duckson", role: "Developer" },
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Record的工作方式相对简单。在这里,它期望数字作为类型,属性值的类型是EmployeeType,因此具有id,fullName和role字段的对象。