我正在尝试使用带有TypeScript的ES2015模块语法来编写一些类。每个类都在.d.ts
文件中实现一个接口(interface)。
这是问题的MWE。
在.d.ts
文件中,我有:
interface IBar {
foo: IFoo;
// ...
}
interface IFoo {
someFunction(): void;
// ...
}
我的导出是:
// file: foo.ts
export default class Foo implements IFoo {
someFunction(): void {}
// ...
}
// no errors yet.
我的导入是:
import Foo from "./foo";
export class Bar implements IBar {
foo: IFoo = Foo;
}
这里的错误是:
error TS2322: Type 'typeof Foo' is not assignable to type 'IFoo'.
Property 'someFunction' is missing in type 'typeof Foo'.
这里有什么想法吗?
最佳答案
当您说foo: IFoo = Foo;
时,您正在将Foo
类分配给IFoo
。但是,IFoo
接口(interface)由该类的实例实现。您需要做:
foo: IFoo = new Foo;
关于javascript - TypeScript TS2322:不能将 'typeof Foo'类型分配给 'IFoo'类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33536116/