Marko Kacanski
Marko Kacanski

Reputation: 433

Cannot import an exported class from a typescript file

I have a class exported from a typescript file called foo.ts

export default class Foo{
}

And I'm trying to import it in another file in the same directory

import {Foo} from './foo';

Which gives me an error

Module '"foo"' has no exported member 'Foo'.

I've looked around online to solve this, but closest I've got is this github issue https://github.com/Microsoft/TypeScript/issues/16475 which is still unresolved at the time of writing.

Am I missing something obvious here? Shouldn't this be valid code?

Upvotes: 0

Views: 1619

Answers (1)

Nurbol Alpysbayev
Nurbol Alpysbayev

Reputation: 21961

These are the options to export/import things:

Default Export

// foo.js
export default class Foo{
}
// bar.js
import Foo from './foo'

Named Export

// foo.js
export class Foo{
}
// bar.js
import {Foo} from './foo'

Have a look at the above and you can see that you've mixed the two approaches in the wrong way.

Upvotes: 3

Related Questions