0
让我们说我们有这样的代码:兼容类型的函数
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());
打字稿不告知错误。打字稿是一个有效的代码。 我曾尝试过在打字游乐场。 (1.8版本)
如何强制打字稿禁止打印?
例如,在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);
}
很好的答案。我的结论是,我的情况是'某些运行时错误可能发生在极少数情况下。“ – Oleg