2017-06-14 32 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:房產「富」是不存在的'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方法是靜態的。靜態只能用於不是對象的類。

你的情況:

A.foo("hello); //works 
new A().foo("hello"); //doesn't work since it's an instance of A