2016-05-24 15 views
1

ここに簡単な例があります。typescriptコンパイラで不正な型が検出されませんでしたか?

私はMVCアーキテクチャの設計のこの種を使用してい

:あなたはビューとしてモデルとV(および派生クラス)とM(および派生クラス)を考えることができます:

abstract class M { abstract update() : void; } 
abstract class V { abstract updateView (m: M) : void; } 

class M1 implements M { 
    fV: V; 
    fName: string; 
    constructor() { this.fV = new V1(); this.fName="M1"; } 
    getName() : string { return this.fName; } 
    update() : void { this.fV.updateView (this); } 
} 

class M2 implements M { 
    fV: V; 
    constructor() { this.fV = new V1(); } 
    update() : void { this.fV.updateView (this); } 
} 

// ==> V1 implementation of V is incorrect but not detected by the compiler 
class V1 implements V { 
    updateView (m: M1) : void { console.log (m.getName() + ": update called"); } 
} 

var m1 = new M1(); 
m1.update(); 

// ==> incorrect use of V1 by M2 not detected at compile time, generates an error at run time 
var m2 = new M2(); 
m2.update(); 

。なお、コンパイル時に次のエラーが発生します。

最初のコードに問題がありますか、コンパイラに問題がありますか?変更は、VはMを期待するので、Xは、(Mを拡張する必要があるということです。これは、(1.8.10 TS)私のためにコンパイルあなたの助けを事前に

おかげ

ドム

答えて

0

abstract class M { abstract update() : void; } 
abstract class V { abstract updateView (m: M) : void; } 

abstract class X extends M { abstract amethod() : void; } 
class V2 implements V { 
    updateView (m: X) : void { console.log ("V2 update called"); } 
} 

in updateView

+0

もちろん、わかっています。問題は2番目の部分(私はそれをコンパイルしたくない)ではなく、最初の例では:私はコンパイルすることも望んでいません(でもそうです)。 –

関連する問題