【发布时间】:2016-08-01 21:43:04
【问题描述】:
假设我们有这个代码:
class MyEvent {
timestamp:number;
}
class AEvent extends MyEvent{
x:number;
y:number;
}
class BEvent extends MyEvent{
key:string;
}
var fn:(event:MyEvent)=>void;
function AE(event:AEvent){
let x = event.x; //Ok, but BEvent hasn't 'x' property.
}
fn = AE;
fn(new BEvent());
Typescript 不会通知错误。对于打字稿,它是一个有效的代码。 我在打字稿操场上试过了。 (1.8版本)
如何强制 typescript 禁止它?
例如,在 C++ 中
class Base {
public:
int timestamp;
};
class B: public Base {
public:
char key;
};
class A: public Base {
public:
int x;
int y;
};
void fA(A *ptr) {}
void (*fn)(Base *ptr);
int main()
{
A *a = new A();
B *b = new B();
fn = fA; //error: invalid conversion from 'void (*)(A*)' to 'void (*)(Base*)'
fn(b);
}
【问题讨论】:
标签: typescript