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?
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With