索引签名
interface StringArray{
[index:number]:string
}
interface NumberDictionary{
[index:string]:number
length:number
//name:string 报错 它和[index:string]重合了, 但是类型却不一致
//[index:string]:number|string //使用联合类型解决
}
const person:NumberDictionary = {
length:1,
qqb:111,
21:123
}
索引签名 不管是 数字类型还是string 它们是一样,也就是当你使用了string的索引签名后 不再被允许使用number类型
继承
interface BasicAddress{
name?:string
street:string
}
interface AddressWithUnit extends BasicAddress{
unit:string
}
interface Colorful {
color: string;
}
interface Circle {
radius: number;
}
interface ColorfulCircle extends Colorful, Circle {}
交叉类型
interface Colorful {
color: string;
}
interface Circle {
radius: number;
}
type ColorfulCircle = Colorful & Circle;
function draw(circle: Colorful & Circle) {
console.log(`Color was ${circle.color}`);
console.log(`Radius was ${circle.radius}`);
}