Typescript export vs. default export
Default Export (export default
)
// MyClass.ts -- using default export
export default class MyClass { /* ... */ }
The main difference is that you can only have one default export per file and you import it like so:
import MyClass from "./MyClass";
You can give it any name you like. For example this works fine:
import MyClassAlias from "./MyClass";
Named Export (export
)
// MyClass.ts -- using named exports
export class MyClass { /* ... */ }
export class MyOtherClass { /* ... */ }
When you use a named export, you can have multiple exports per file and you need to import the exports surrounded in braces:
import { MyClass } from "./MyClass";
Note: Adding the braces will fix the error you're describing in your question and the name specified in the braces needs to match the name of the export.
Or say your file exported multiple classes, then you could import both like so:
import { MyClass, MyOtherClass } from "./MyClass";
// use MyClass and MyOtherClass
Or you could give either of them a different name in this file:
import { MyClass, MyOtherClass as MyOtherClassAlias } from "./MyClass";
// use MyClass and MyOtherClassAlias
Or you could import everything that's exported by using * as
:
import * as MyClasses from "./MyClass";
// use MyClasses.MyClass and MyClasses.MyOtherClass here
Which to use?
In ES6, default exports are concise because their use case is more common; however, when I am working on code internal to a project in TypeScript, I prefer to use named exports instead of default exports almost all the time because it works very well with code refactoring. For example, if you default export a class and rename that class, it will only rename the class in that file and not any of the other references in other files. With named exports it will rename the class and all the references to that class in all the other files.
It also plays very nicely with barrel files (files that use namespace exports—export *
—to export other files). An example of this is shown in the "example" section of this answer.
Note that my opinion on using named exports even when there is only one export is contrary to the TypeScript Handbook—see the "Red Flags" section. I believe this recommendation only applies when you are creating an API for other people to use and the code is not internal to your project. When I'm designing an API for people to use, I'll use a default export so people can do import myLibraryDefaultExport from "my-library-name";
. If you disagree with me about doing this, I would love to hear your reasoning.
That said, find what you prefer! You could use one, the other, or both at the same time.
Additional Points
A default export is actually a named export with the name default
, so if the file has a default export then you can also import by doing:
import { default as MyClass } from "./MyClass";
And take note these other ways to import exist:
import MyDefaultExportedClass, { Class1, Class2 } from "./SomeFile";
import MyDefaultExportedClass, * as Classes from "./SomeFile";
import "./SomeFile"; // runs SomeFile.js without importing any exports
What does export default do in JSX?
Export like export default HelloWorld;
and import, such as import React from 'react'
are part of the ES6 modules system.
A module is a self contained unit that can expose assets to other modules using export
, and acquire assets from other modules using import
.
In your code:
import React from 'react'; // get the React object from the react module
class HelloWorld extends React.Component {
render() {
return <p>Hello, world!</p>;
}
}
export default HelloWorld; // expose the HelloWorld component to other modules
In ES6 there are two kinds of exports:
Named exports - for example export function func() {}
is a named export with the name of func
. Named modules can be imported using import { exportName } from 'module';.
In this case, the name of the import should be the same as the name of the export. To import the func in the example, you'll have to use import { func } from 'module';
. There can be multiple named exports in one module.
Default export - is the value that will be imported from the module, if you use the simple import statement import X from 'module'
. X is the name that will be given locally to the variable assigned to contain the value, and it doesn't have to be named like the origin export. There can be only one default export.
A module can contain both named exports and a default export, and they can be imported together using import defaultExport, { namedExport1, namedExport3, etc... } from 'module';
.
export * as bar VS export { default as bar }
This one exports all named exports and default export from foo
as bar
:
export * as bar from 'foo'
This one only exports default export from foo
as bar
:
export { default as bar } from 'foo'
`export const` vs. `export default` in ES6
It's a named export vs a default export. export const
is a named export that exports a const declaration or declarations.
To emphasize: what matters here is the export
keyword as const
is used to declare a const declaration or declarations. export
may also be applied to other declarations such as class or function declarations.
Default Export (export default
)
You can have one default export per file. When you import you have to specify a name and import like so:
import MyDefaultExport from "./MyFileWithADefaultExport";
You can give this any name you like.
Named Export (export
)
With named exports, you can have multiple named exports per file. Then import the specific exports you want surrounded in braces:
// ex. importing multiple exports:
import { MyClass, MyOtherClass } from "./MyClass";
// ex. giving a named import a different name by using "as":
import { MyClass2 as MyClass2Alias } from "./MyClass2";
// use MyClass, MyOtherClass, and MyClass2Alias here
Or it's possible to use a default along with named imports in the same statement:
import MyDefaultExport, { MyClass, MyOtherClass} from "./MyClass";
Namespace Import
It's also possible to import everything from the file on an object:
import * as MyClasses from "./MyClass";
// use MyClasses.MyClass, MyClasses.MyOtherClass and MyClasses.default here
Notes
- The syntax favours default exports as slightly more concise because their use case is more common (See the discussion here).
A default export is actually a named export with the name
default
so you are able to import it with a named import:import { default as MyDefaultExport } from "./MyFileWithADefaultExport";
Does TypeScript support export default Enum?
This is how ES6 works.
enum Hashes {
FOO = 'foo',
BAR = 'bar',
}
export default Hashes;
Exporting it as const? Concerning the default export, there is only a single default export per module. A default export can be a function, a class, an object or anything else. This value is to be considered as the "main" exported value since it will be the simplest to import.
export const enum Hashes {
FOO = 'foo',
BAR = 'bar',
}
TypeScript - export default as
as
has 2 meanings. In ES6 modules, it can be a way to rename an import or export. In TypeScript, it is a typecast operator. I am going to assume you mean the first, specifically a way to rename an export.
Now take a look at this expression you suggested:
export default connect(mapStateToProps, mapDispatchToProps)(HomePage) as HomePage;
Do you want to export it as default
or do you want to export it as HomePage
? When you export default, default
is the export name. Given that, does this make sense to expect it to be called HomePage
? You either export something that is called default
or something that is called HomePage
.
This table associates different export statement forms with the expected export name and local names:
For importing, though...
- If you are importing a default export, you have to explicitly name the import. Default exports will not automatically have a local name after importing. Example:
import express from "express"
. The local name does not matter. Could as well beimport banana from "express"
. - If you are importing a named export, by default, the local name will be the same as the exported name. Example: If you have
export const x = 2
, then, you should import it asimport { x } from "module"
. You can also rename the import like this: `import { x as y } from "module".
The above tables are from the ES6 specs: http://www.ecma-international.org/ecma-262/6.0/
module.exports vs. export default in Node.js and ES6
The issue is with
- how ES6 modules are emulated in CommonJS
- how you import the module
ES6 to CommonJS
At the time of writing this, no environment supports ES6 modules natively. When using them in Node.js you need to use something like Babel to convert the modules to CommonJS. But how exactly does that happen?
Many people consider module.exports = ...
to be equivalent to export default ...
and exports.foo ...
to be equivalent to export const foo = ...
. That's not quite true though, or at least not how Babel does it.
ES6 default
exports are actually also named exports, except that default
is a "reserved" name and there is special syntax support for it. Lets have a look how Babel compiles named and default exports:
// input
export const foo = 42;
export default 21;
// output
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var foo = exports.foo = 42;
exports.default = 21;
Here we can see that the default export becomes a property on the exports
object, just like foo
.
Import the module
We can import the module in two ways: Either using CommonJS or using ES6 import
syntax.
Your issue: I believe you are doing something like:
var bar = require('./input');
new bar();
expecting that bar
is assigned the value of the default export. But as we can see in the example above, the default export is assigned to the default
property!
So in order to access the default export we actually have to do
var bar = require('./input').default;
If we use ES6 module syntax, namely
import bar from './input';
console.log(bar);
Babel will transform it to
'use strict';
var _input = require('./input');
var _input2 = _interopRequireDefault(_input);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
console.log(_input2.default);
You can see that every access to bar
is converted to access .default
.
Related Topics
Why Does Isnan(" ") (String with Spaces) Equal False
How to Break Nested Loops in JavaScript
Dynamically Arrange Some Elements Around a Circle
How to Rotate a 3D Object on Axis Three.Js
Secure Random Numbers in JavaScript
Detecting Line-Breaks with Jquery
Math Random Number Without Repeating a Previous Number
Getting Error "Form Submission Canceled Because the Form Is Not Connected"
Es6: Conditional & Dynamic Import Statements
Sanitizing User Input Before Adding It to the Dom in JavaScript
Element Not Visible Error (Not Able to Click an Element)
What Is Settimeout Doing When Set to 0 Milliseconds
Typescript Recursive Function Composition
How to Sort an Array Without Mutating the Original Array
How to Inherit Old-Style Class from Ecmascript 6 Class in JavaScript
How to Load a JavaScript File Dynamically