采纳答案成功!
向帮助你的同学说点啥吧!感谢那些助人为乐的人
这样写不行么?不一定非要用[P in keyof any]: T吧?
[P in keyof any]: T
type MyRecord<T> = { [index: string]: T }
type MyRecord<T> = Reocrd<T[keyof T], T>;这样写,类型约束更好
// [P in keyof any]这样写更适合Record的特点,因为Record存储的key 有变数,
// 1 修改更方便 2. 适应场景更多
// 1.修改更方便 ==>泛型个数增加灵活
type MyRecordExt<T, M> = {
[P in keyof M]: T
}
// 2.1 适应场景更多
let all: MyRecordExt<Car, any> = {}
// 2.2 适应场景更多
type CarBrandSort = {
"铃木": number,//数量
"丰田": number
type Car = { id: number, price: number, brandno: string }
let record: MyRecordExt<Car, CarBrandSort> = {
"铃木": { id: 1, price: 100000, brandno: "雨燕" },
"丰田": { id: 2, price: 500000, brandno: "凯美瑞" }
登录后可查看更多问答,登录/注册
轻松驾驭 TypeScript 高级用法, 突破前端成长瓶颈
1.2k 15
938 12
1.1k 11
978 11
736 10