2017-08-02 12 views
2

はコードTypeScriptは取得専用のプロパティを尊重していませんか?

class A { 
    private _f: string; 
    get f(): string { 
     return this._f; 
    } 
} 
class B { 
    f: string;  
    static x(a: A): B { 
     // I expect an error in next line because A does not have a setter for 'f' 
     return a; 
    } 
} 
let a = new A(); 
// this line gives error, as expected 
a.f = "safe"; 
let b = B.x(a); 
// this will mutate object that I want to be immutable! 
b.f = "ouch!"; 

このエラーが発生する理由、次の点を考慮しますか? (TS 2.4と2.3で試行)。

答えて

関連する問題