Typescript Record的用法

Record<Keys,Type>

构造一个对象类型,其属性key是Keys,属性value是Tpye。被用于映射一个类型的属性到另一个类型

interface CatInfo {
    age: number,
    breed: string
}
type CatName = 'miffy'| 'boris' | 'mordred' // 字符串字面量类型
const cats: Record<CatName, CatInfo> ={
    miffy: {age: 10, breed: "Persian"},
    boris: {age:5, breed: 'Maine Coon'},
    mordred: {age: 16, breed: 'British Shorthair'}
};
cats.boris;


来自为知笔记(Wiz)


posted on 2021-08-01 22:21  白衣风云  阅读(1944)  评论(0编辑  收藏  举报

导航