-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write a FAQ entry for export =
vs export default
/ import
variants
#7185
Comments
I've actually been trying to write this up today as part of fixing up the module bugs assigned to me. I could extract out the general info from the internal info as the basis. Maybe I'll regret this... but feel free to assign to me if someone is not already working on it. |
We should put any additional information for this in the handbook module page: https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Modules.md we can also add a link to that page in the FAQ page. |
Looks good! A couple of things that would be really useful additions:
|
I do not know, who is responsible for the handbook on typescriptlang.org, but the chapter "Going external" should also cover this topic, or at least a hint: |
I'd add |
+1 pls. I dont even know the answer now that finding this issue :/ |
TL;DR:
// ./a.js
module.exports = { a: 10, b: 20 };
// ./b.js
let a = require('./a'); A lot of the time, people will have exactly one thing that they're exporting, like a function: // ./a.js
module.exports = function foo() { /*...*/ }
// ./b.js
var a = require('./a');
a();
// a.js
export default function foo() {
// ...
}
// b.js
import a from './a.js';
a(); But it's just special syntax; really, import * as a from './a.js';
a.default();
import { default as aFujnc } from './a.js'
aFunc(); |
export default ... (Default Export)// calculator.ts // compiled.js
// ============= // ===========
export default class Calculator { // var Calculator = /** @class */ (function () {
public add(num1, num2) { // function Calculator() {}
return num1 + num2; // Calculator.prototype.add = function (num1, num2) {
} // return num1 + num2;
} // };
// return Calculator;
// }());
// exports["default"] = Calculator; import ... from "module";// importer.ts // compiled.js
// =========== // ===========
import Calculator from "./calculator"; // exports.__esModule = true;
// var calculator = require("./calculator");
let calc = new Calculator(); // var calc = new calculator["default"]();
// console.log(calc.add(2, 2));
console.log(calc.add(2, 2)); // Notes:
export = ...// calculator.ts // compiled.js
// ============= // ===========
export = class Calculator { // module.exports = /** @class */ (function () {
public add(num1, num2) { // function Calculator() {}
return num1 + num2; // Calculator.prototype.add = function (num1, num2) {
} // return num1 + num2;
} // };
// return Calculator;
// }()); import ... = require("module");// importer.ts // compiled.js
// =========== // ===========
import Calculator = require("./calculator"); // exports.__esModule = true;
// var Calculator = require("./calculator");
let calc = new Calculator(); // var calc = new Calculator();
// console.log(calc.add(2, 2));
console.log(calc.add(2, 2)); // Notes:
export ... (Named Export)// calculator.ts // compiled.js
// ============= // ===========
export class Calculator { // exports.__esModule = true;
public add(num1, num2) { // var Calculator = /** @class */ (function () {
return num1 + num2; // function Calculator() {}
} // Calculator.prototype.add = function (num1, num2) {
} // return num1 + num2;
// };
// return Calculator;
// }());
// exports.Calculator = Calculator; import { ... } from "module";// importer.ts // compiled.js
// =========== // ===========
import { Calculator } from "./calculator"; // exports.__esModule = true;
// var calculator = require("./calculator");
let calc = new Calculator(); // var calc = new calculator.Calculator();
// console.log(calc.add(2, 2));
console.log(calc.add(2, 2)); // Notes:
|
|
import = require does not work for me
|
This is most likely the wrong forum for your issue. I would suggest either Stack Overflow or creating a new issue. |
I've been using TS for over a year and I'm still confused about the below. I've thrown it on stackoverflow but I bet it'll be helpful for this documentation as well.
I resolve this issue with the following but I know its not the same nor what I want.
|
The source code uses `module.exports = ...`, not `exports.default = ...`: https://github.com/nunofgs/clean-deep/blob/9d8313db868b239b990f9db352215844ad7ae65a/src/index.js#L14-L22 Given this, TS definitions should use `export =`, not `export default`. See microsoft/TypeScript#7185 (comment). This matters when the project is compiled _without_ the `"esModuleInterop": true` flag. In that case, doing ```ts import cleanDeep from 'clean-deep' cleanDeep(...) ``` would throw an error like `TypeError: clean_deep_1.default is not a function`.
- Add types entry exports[0]. I'm sad TypeScript doesn't seem to be using the plain old types field but it isn't. - Replace index.d.ts' `export default` with `export =`[1]. From what I can tell, `module.exports` is always used for all entrypoints so this is the correct definition. - Use the Worker var declaration from lib.dom.d.ts. I think this is fine since the old definition already used the Worker interface from the same file. This typing is more accurate, easier to read,d less likely to become outdated, and less code. Bug: developit#20 developit#22 [0]: https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#packagejson-exports-imports-and-self-referencing [1]: microsoft/TypeScript#7185 (comment)
// a.ts let a = 0;
export default a;
setInterval(() => {
a++;
}, 1000); ===================================== export let b = 0;
setInterval(() => {
b++;
}, 1000); ==================================== import a from './a';
import { b } from './b';
setInterval(() => {
console.log('a =', a);
console.log('b =', b);
console.log('====');
console.log();
}, 1000); // tsconfig.json {
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"baseUrl": "."
}
} Run: |
Moving this over to the proper was TS does things for CJS, rather than treating it like `export default`, which I was mistakenly doing previously. microsoft/TypeScript#7185 Listening to Blackwater Park, Blackwater Park.
Moving this over to the proper was TS does things for CJS, rather than treating it like `export default`, which I was mistakenly doing previously. microsoft/TypeScript#7185 Listening to Blackwater Park, Blackwater Park.
This is now an entire set of module docs 🥰 See https://www.typescriptlang.org/docs/handbook/modules/introduction.html |
People don't understand the difference between these things:
People also don't understand the difference between these things:
We need to write a comprehensive answer that explains which import statements correctly acquire which exports, and also describe auto-lifting behavior present in some loaders.
The text was updated successfully, but these errors were encountered: