Record用来定义对象的键和值,也就是key和value。
例子:
interface PageInfo {
title: string
}
type Page = "home" | "about" | "contact";
const nav : Record<Page, PageInfo> = {
about: {title, "about"},
home: {title, "home"},
contact: {title, "contact"}
};
Record后面的泛型就是键和值,假如我们需要一个对象,他有名字、性别、身高三个属性,属性值必须是字符串,那么可以这样写:
type keys = "name" | "sex" | "height"
const person : Record<keys, string> = {
name: '小明',
sex: '男',
height: '188'
}
标签:about,const,title,TS,Record,contact,home
From: https://www.cnblogs.com/guanyyang/p/17091243.html