You don't have to specify the type of the export - you have to specify the type of the local binding in your module.
there should be named exports and default exports.
There are:
export {localX as exportedX};
export {localX as default};
All those examples you've given are actually shorthands, which both declare a local variable and export it under the same name:
var myVar1 = …;
let myVar2 = …;
const MY_CONST = …;
function myFunc() {
…
}
function* myGeneratorFunc() {
…
}
class MyClass {
…
}
export {
myVar,
myVar2,
MY_CONST,
myFunc,
myGeneratorFunc,
myClass
};
What difference can it make to export const
? When you import a module it's immutable anyway.
That you cannot reassign it inside your module. The export doesn't export a value1, it exports a binding to your local variable. Imports actually aren't immutable, they are only non-writable.
// example.js
export var value; // this one would not work with `const`
export default function(x) {
value = x;
}
// main.js
import write, {value} from 'example';
console.log(value); // undefined
write(42);
console.log(value); // 42
1: Default exports are a bit special in that regard. The export default …
declaration does indeed allow you to directly export the value of an expression (or an anonymous function/function*/class declaration), but behind the scenes it actually does create a local variable in your module with the name *default*
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…