0

我发现了一个场景,我可以可靠地让 TypeScript 编译器失败并显示错误消息:“内部错误:无法获取属性‘publicMembers’的值:对象为空或未定义”

这是我的 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 编译器崩溃的副本: publicMembers 为 null 或 undefined,但重现明显不那么复杂,所以我想我还是会继续发布它。

有什么想法吗?

4

3 回答 3

3

我已将此添加到Codeplex 上的错误中

如果您还没有证明它对您来说也是一个问题,那么您应该投票支持该错误。

你是对的,没有什么可以添加到答案中的——这是编译器中的一个错误。我们只需要等待修复。

于 2012-11-12T23:58:48.007 回答
2

另一种解决方法。声明void方法的返回类型:

private handleEventFromSource1(signalState): void { ... }
private handleEventFromSource2(event): void { ... }
于 2012-11-13T07:22:08.160 回答
1

对于它的价值,到目前为止我发现的问题的最佳解决方法(直到他们修复编译器错误)是避免命名的回调接口。换句话说,这段代码工作得很好:

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.");
    }
}
于 2012-11-13T01:18:31.547 回答