2017-06-14 30 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