TypeScript TS2322: Type 'typeof Foo' not assigned to type 'IFoo'

I am trying to compose some classes using ES2015 module syntax using TypeScript. Each class implements an interface in a .d.ts file.

Here are the MWE problems.

In the .d.ts file I have:

 interface IBar { foo: IFoo; // ... } interface IFoo { someFunction(): void; // ... } 

My export:

 // file: foo.ts export default class Foo implements IFoo { someFunction(): void {} // ... } // no errors yet. 

And my import:

 import Foo from "./foo"; export class Bar implements IBar { foo: IFoo = Foo; } 

The error is here:

 error TS2322: Type 'typeof Foo' is not assignable to type 'IFoo'. Property 'someFunction' is missing in type 'typeof Foo'. 

Any ideas here?

+8
javascript ecmascript-6 typescript
source share
1 answer

When you say foo: IFoo = Foo; , you assign the class Foo to IFoo . However, the IFoo interface IFoo implemented by instances of this class. You need to do:

 foo: IFoo = new Foo; 
+11
source share

All Articles