2017-06-14 4 views
0

インターフェイスの型のようなクラスを使用できますか?例えば、私はクラスの動物を持って、私のようなものを使用することができます:私はこのような状況でエラーエン持っクラスはTypescriptのインターフェイスの型のようです

interface I { 
    object: Animal 
} 

を:

class A { 
    public static foo(text: string): string { 
     return text; 
    } 
    } 

interface IA { 
    testProp: A; 
    otherProp: any; 
} 

class B { 
    constructor(prop: IA) { 
     console.log(prop.otherProp); 
     console.log(prop.testProp.foo('hello!')); 
    } 
} 

TS2339:プロパティ「fooが」オン存在しません。タイプ 'A'

答えて

0

あなたはtypeof Aを使用する必要があります。

class A { 
    public static foo(text: string): string { 
     return text; 
    } 
} 

interface IA { 
    testProp: typeof A; 
    otherProp: any; 
} 

class B { 
    constructor(prop: IA) { 
     console.log(prop.otherProp); 
     console.log(prop.testProp.foo('hello!')); 
    } 
} 
+1

は、この作品をありがとう:あなたのケースで

0

コードの問題は、fooメソッドが静的であることです。 Staticは、オブジェクトではないクラスに対してのみ使用できます。

A.foo("hello); //works 
new A().foo("hello"); //doesn't work since it's an instance of A 
関連する問題