我发现了一个场景,我可以可靠地让TypeScript编译器失败并显示错误消息:“内部错误:无法获取属性'publicMembers'的值:object is null或undefined”
这是我的Repro.ts文件:
interface Callback { (data: any): void; }
class EventSource1 {
addEventHandler(callback: Callback): void { }
}
class EventSource2 {
onSomeEvent: Callback;
}
export class Controller {
constructor () {
var eventSource = new EventSource1();
// Commenting the next line will allow it to compile.
eventSource.addEventHandler(msg => this.handleEventFromSource1(msg));
}
private handleEventFromSource1(signalState) {
console.log('Handle event from source 1');
var eventSource2 = new EventSource2();
// Commenting the next line will allow it to compile.
eventSource2.onSomeEvent = msg => this.handleEventFromSource2(msg);
}
private handleEventFromSource2(event) {
console.log("Handling event from source 2.");
}
}
这很可能是TypeScript compiler crash: publicMembers is null or undefined的副本,但是复制品的复杂程度要低得多,所以我想我会继续发布它。
有什么想法吗?
答案 0 :(得分:3)
我已将此添加到bug over on Codeplex。
如果您还没有证明这也是一个问题,您应该投票支持该错误。
由于你是对的,没有什么可以添加到答案中 - 这是编译器中的一个错误。我们只需要等待修复。
答案 1 :(得分:2)
另一种解决方法。为方法声明void
返回类型:
private handleEventFromSource1(signalState): void { ... }
private handleEventFromSource2(event): void { ... }
答案 2 :(得分:1)
对于它的价值,到目前为止我找到的问题的最佳解决方法(直到他们修复编译器错误)是避免命名的Callback接口。换句话说,这段代码运行得很好:
class EventSource1 {
addEventHandler(callback: { (data: any): void; }): void { }
}
class EventSource2 {
onSomeEvent: { (data: any): void; };
}
class Controller {
constructor () {
var eventSource = new EventSource1();
eventSource.addEventHandler(msg => this.handleEventFromSource1(msg));
}
private handleEventFromSource1(signalState) {
console.log('Handle event from source 1');
var eventSource2 = new EventSource2();
eventSource2.onSomeEvent = msg => this.handleEventFromSource2(msg);
}
private handleEventFromSource2(event) {
console.log("Handling event from source 2.");
}
}