Sam Genest
Sam Genest

Reputation: 117

Importing modules without a default export in TypeScript (CommonJS)

Is it possible to import a module defined without a default export via: import module from 'module'; and compile it to commonjs?

This Stack Overflow Answer suggests that it is possible with the --allowSyntheticDefaultImports option passed (albeit only for systemjs modules?). The Compiler Options Documentation states that allowSyntheticDefaultImports only affects typechecking.

Are there any work arounds besides the import * from module as 'module'; syntax?

Upvotes: 1

Views: 2775

Answers (1)

Badacadabra
Badacadabra

Reputation: 8497

What you are describing is not CommonJS...

CommonJS is the module API implemented by Node, where you use module.exports, exports and require to manage your modules.

TypeScript is a superset of JavaScript and relies on ES6 native modules. So if you do not want default exports, you should be able to do something like this:

Your module

export function foo() {
  console.log('Foo');
};

export function bar() {
  console.log('Bar');
};

Your entry point

import {foo, bar} from './module';

foo();
bar();

Upvotes: 1

Related Questions