2012-11-12 35 views
0

我發現一種情況,我可以可靠地獲得打字稿編譯器失敗,出現錯誤信息:「內部錯誤:無法獲取財產「publicMembers」的值:對象爲null或undefined」打字稿編譯器錯誤:「無法獲得屬性‘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 compiler crash: publicMembers is null or undefined重複,但攝製是顯著不那麼複雜,所以我想我會繼續併發布它。

有什麼想法?

回答

3

我已將此添加到bug over on Codeplex

如果您還沒有證明它對您也是一個問題,您應該爲該bug投票。這是在編譯器中的錯誤 -

,你是對的,那裏是沒有多少人加入到一個答案。我們只需要等待修復。

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

另一種解決方法。聲明void返回類型的方法:

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

有幫助,謝謝。比我的建議更好。 –

相關問題