interface CanFly { fly(): void; }
我将创建两个类, Bird AirPlane
Bird
AirPlane
最后,我想创建一个函数 flyIn2Seconds ,如下所示:
flyIn2Seconds
function flyIn2Seconds(who: any extends CanFly) { setTimeout(() => who.fly(), 2000); }
然而 any extends CanFly 不起作用( [ts] '?' expected. ). 有没有办法指定类型“any class that implements CanFly”?
any extends CanFly
[ts] '?' expected.
只需指定接口:
function flyIn2Seconds(who: CanFly) { setTimeout(() => who.fly(), 2000); }
更多 the interfaces documentation