Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript TS2322: Type 'typeof Foo' is not assignable to type 'IFoo'

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

Here is a MWE of the problem.

In a .d.ts file I have:

interface IBar {
  foo: IFoo;
  // ...
}

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

My export is:

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

And my import is:

import Foo from "./foo";

export class Bar implements IBar {
   foo: IFoo = Foo;
}

The error here is:

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

Any ideas here?

like image 527
azz Avatar asked Nov 05 '15 03:11

azz


People also ask

How to fix Type undefined is not assignable to Type?

The "Type 'undefined' is not assignable to type" error occurs when a possibly undefined value is assigned to something that expects a different type. To solve the error, use the non-null assertion operator or a type guard to verify the value is of the specific type before the assignment.

Is not assignable to Type model?

The "Type 'string' is not assignable to type" TypeScript error occurs when we try to assign a value of type string to something that expects a different type, e.g. a more specific string literal type or an enum. To solve the error use a const or a type assertion. Here is the first example of how the error occurs.


1 Answers

When you say foo: IFoo = Foo; you are assigning the class Foo to IFoo. However the interface IFoo is implemented by instances of that class. You need to do :

foo: IFoo = new Foo;
like image 131
basarat Avatar answered Oct 07 '22 16:10

basarat