老师,可索引类型那里的Animal和Dog可不可以这样理解:
//相当于Dog 继承了Animal的name
interface a {
breed:string,
name:string
}
//相当于Animal
interface b{
name:string
}
interface c {
[index:number]:a
[index:string]:b
}
interface d {
[index:number]:b //报错 数字索引类型“b”不能赋给字符串索引类型“a”
[index:string]:a
}
结论:
以number查索引值可以隐式转换为string,相当于可以通过number,string 类型查索引值而string查索引值,则只能通过string查索引值
string,nubmer 继承于string
string,number为"子" --> Dog
string为“父” --> Animal
//类似的例子
interface two{
x:number,
y:number
}
interface three{
x:number,
y:number,
z:number
}
//这里two是three的父类
let x:two = {x:1,y:2}
let y:three = {x:1,y:2,z:4}
x =y //OK
y =x //报错,需要z