Omar Huseynov Omar Huseynov - 2 months ago 8
TypeScript Question

TypeScript can't resolve exported default class

I have following module in _M01.ts:

export module _M01 {

export default class Foo {

constructor () {}

}

}


Then when I want to import the above module in my _M02.ts I get following error:

import Foo from "./_M01"; // error "./_M01" has no default export


Is it an IDE problem or TS compiler failing or is it just simply me not understanding how this stuff works?

Answer

You don't need the module wrapper around your class in _M01, so this will do what you want

_M01.ts:

export default class Foo {
    constructor () {}
}

_M02.ts

import Foo from "./_M01"; //This works

There's more information here, but the TLDR versions is that you don't use the Module keyword anymore in TS. You can define a Namespace instead, however for what you need the simple example above is fine.

Comments