Repository navigation
A ES6 module with with multiple exports including a default export #2440
Description
Activity
JsonFreeman commented
on Mar 21, 2015 ContributorMore actionsFor 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 #2242 (comment).
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();
Reacted by tarik02, Albert Vàzquez, Bhoomtawath Plinsut, Lorenzo Marcon, Yevhenii Halenko, Vyacheslav, Spencer Kaiser, Amit Jakubowicz, Vivek C S and Michael McQuadeReacted by Spencer KaiserReacted by Spencer KaiserI 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 namedefault. So, when youimport *from a module with a default export, the default export is available as a member nameddefault.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();
Reacted by Yiling, CosmoX, Dmitri, Yevhenii Halenko, Mohammad Mohibbullah, Kryštof Řeháček, Vivek C S and Michael McQuade- addedQuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code
on Mar 22, 2015 - added a commit that references this issue
on Sep 14, 2017 Hi, sorry for digging out an old thread. I've recently thought of moving into TS
2.8and 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 Typesand single types separately.Would you mind giving me a tip on whats the valid syntax at this moment? Thanks!
Reacted by Andras TothWojciech Jeremy Połowniak (@Worie) i can't find either.
but with one more line i can do:
import * as React from 'react'; const { Component } = React;
- locked and limited conversation to collaborators
on Jul 25, 2018
I'm not sure if this is valid ES6 code but I'm trying to get this work:
app.ts
module1.ts
I'm using today's master branch and I get the following error when I compile
app.ts:An another question, if
app.tsdo a namespaced import will I have access to the default function too?app.ts