索引类型,定义key:value,key和value可以定义为任何类型;通常定义数组对象
interface Person {
name: string
age: number
}
const personInfos: Person = { name: 'a', age: 16 }
// 》》》》》》》》》》 索引类型
interface Student {
[id: number]: Person // id只是个站位符
}
const sudent: Student = {
0:{ name:'a', age: 1 }
}
// 映射类型
利用操作符 keyof typeof,根据已有类型创建新的类型
type Keys = keyof Person // 'name' | 'age' 联合类型的字面量
typeof 针对的是 ---- 变量值
type Ty = typeof personInfos // --- { name: string; age: number }

映射类型

typeof只能是值或者枚举

索引
const products = {
1: {
name:'nick',
price:200
},
2:{
name: 'adidas',
price:300
}
}
interface Product {
name:string
price: number
}
//索引类型
interface Products {
[id:number]:Product
}
//映射类型
type Keys = keyof Product;
type Tp = typeof products;


// 索引类型
const product = {
name: 'Nick',
price: 100
}
const products = {
1: {
name: 'Nick',
price: 100
},
2: {
name: 'adidas',
price: 150
}
}
interface Product {
name: string,
price: number
}
interface Products {
[id: number]: Product
}
// 定义对象时
interface Obj {
[key: string | number]: string | number | boolean | {} | null | undefined;
}
// 映射类型
// 通过已有类型创建新类型、通过操作符创建新类型
// keyof
type Keys = keyof Product; // 'name' | 'price' 以对象类型的key 的字面量作为新类型。
type Tp = typeof product; // typeof 后跟的是具体值,不能跟类型。typeof 获取值的类型后,用来创建新的类型。
const keys: Keys = 'name';
const tp: Tp = {
name: 'lining',
price: 100
}