TS

请看代码:

interface Animal {
    typ: string;
}
class Duck implements Animal {
    typ: string = "鸭子"
}
class Goose implements Animal {
    typ: string = "鹅"
}
class Fish implements Animal {
    typ: string = "鱼"
}

// 鸭子和鹅可以飞,但是鱼不行
type canFly = (Duck | Goose);

// 必须是可以飞的动物
function fly(animal: canFly): void {
    console.log(animal.typ + '飞走了');
}

let duck = new Duck();
fly(duck);

let goose = new Goose();
fly(goose);

// 不知道为什么期待的报错没有出现
let fish: Fish = new Fish();
fly(fish);

求大神解答

待解决 悬赏分:10 - 离问题结束还有
反对 0举报 0 收藏 0

我来回答

回答1