Typescript: A ES6 module with with multiple exports including a default export

Created on 20 Mar 2015  路  4Comments  路  Source: microsoft/TypeScript

I'm not sure if this is valid ES6 code but I'm trying to get this work:
app.ts

import function1, { function2 } from './module1'

function1()
function2()

module1.ts

export default function function1() {
  console.log('hej')
}

export function function2() {
  console.log('hej')
}

I'm using today's master branch and I get the following error when I compile app.ts:

error TS2309: An export assignment cannot be used in a module with other exported elements.

An another question, if app.ts do a namespaced import will I have access to the default function too?

app.ts

import * as module1 from './module1'

module1.function1()
module2.function2()
Question

Most helpful comment

For your first question, yes it is valid ES6 code. It gives an error in the compiler today, but we plan to change the design to allow that code, per https://github.com/Microsoft/TypeScript/issues/2242#issuecomment-83694181.

For your second question, no. An import * does not include the default export, so you will not have access to function1. However, you can access the default export via an explicit namespace import, like this:

import { default as function1, function2 } from './module1';

function1();
function2();

You can also combine the default import with *:

import function1, * as module1 from './module1';

function1();
module1.function2();

All 4 comments

For your first question, yes it is valid ES6 code. It gives an error in the compiler today, but we plan to change the design to allow that code, per https://github.com/Microsoft/TypeScript/issues/2242#issuecomment-83694181.

For your second question, no. An import * does not include the default export, so you will not have access to function1. However, you can access the default export via an explicit namespace import, like this:

import { default as function1, function2 } from './module1';

function1();
function2();

You can also combine the default import with *:

import function1, * as module1 from './module1';

function1();
module1.function2();

I just posted #2460 which addresses this issue.

Regarding import * and default exports, keep in mind that a default export is simply an export with the reserved name default. So, when you import * from a module with a default export, the default export is available as a member named default.

Given your example above, you can import and use the module as follows:

import * as module1 from './module1'

module1.default();  // This calls function1
module2.function2();

If you want to export a member both by its name and as the default, you can do this:

export function function1() {
  console.log('hej')
}

export function function2() {
  console.log('hej')
}

export default function1;

Then you can do this:

import * as module1 from './module1'

module1.default();  // This calls function1
module1.function1();
module2.function2();

Hi, sorry for digging out an old thread. I've recently thought of moving into TS 2.8 and I cant seem to find a way to combine import into something like that:

import Types, { TypeA, TypeB } from '@/common/support/types';

I mean, importing * as Types and single types separately.

Would you mind giving me a tip on whats the valid syntax at this moment? Thanks!

@Worie i can't find either.

but with one more line i can do:

import * as React from 'react';

const { Component } = React;
Was this page helpful?
0 / 5 - 0 ratings

Related issues

MartynasZilinskas picture MartynasZilinskas  路  3Comments

Zlatkovsky picture Zlatkovsky  路  3Comments

DanielRosenwasser picture DanielRosenwasser  路  3Comments

jbondc picture jbondc  路  3Comments

kyasbal-1994 picture kyasbal-1994  路  3Comments