【问题标题】:Typescript compiler error: "Unable to get value of the property 'publicMembers': object is null or undefined"打字稿编译器错误:“无法获取属性'publicMembers'的值:对象为空或未定义”
【发布时间】:2012-11-01 10:14:36
【问题描述】:

我发现了一种情况,我可以可靠地使 TypeScript 编译器失败并显示错误消息:“内部错误:无法获取属性 'publicMembers' 的值:对象为 null 或未定义”

这是我的 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 的复制品,但复制品的复杂性要低得多,所以我想我还是会继续发布它。

有什么想法吗?

【问题讨论】:

    标签: typescript tsc


    【解决方案1】:

    我已将此添加到bug over on Codeplex

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

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

    【讨论】:

      【解决方案2】:

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

      private handleEventFromSource1(signalState): void { ... }
      private handleEventFromSource2(event): void { ... }
      

      【讨论】:

        【解决方案3】:

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

        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.");
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-05-24
          • 1970-01-01
          • 2012-06-11
          • 2013-04-24
          • 1970-01-01
          • 2011-09-30
          • 2011-08-12
          相关资源
          最近更新 更多