Class 类类似于Typescript接口中的类型
可以像在接口中使用类型一样使用类吗?例如,我有一个类动物,我可以使用类似于:Class 类类似于Typescript接口中的类型,class,typescript,types,interface,Class,Typescript,Types,Interface,可以像在接口中使用类型一样使用类吗?例如,我有一个类动物,我可以使用类似于: interface I { object: Animal } 在这种情况下,我有一个错误: class A { public static foo(text: string): string { return text; } } interface IA { testProp: A; otherProp: any; } class B { cons
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”上不存在属性“foo”您需要使用
类型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!'));
}
}
代码中的问题是foo方法是静态的。静态只能用于类而不是对象 就你而言:
A.foo("hello); //works
new A().foo("hello"); //doesn't work since it's an instance of A