You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue matches PR #30. I think it's better to have a discussion here.
I believe this issue applies to all your packages that recently added types via d.ts files. I assume that the purpose was to improve intellisense. Right now, it works fine for Typescript users who have --esModuleInterop turned on, but for normal JS use it's broken:
constmem=require('./mem')
isn't callable, even though the primary export of this package is a function.
Instead, people have to change their code to
constmem=require('./mem').default
which seems like it goes against the normal commonjs usage.
The d.ts file seems like it exists solely to provide better intellisense, and it consists solely of typescript syntax, so I think using the typescript syntax for commonjs exports make sense. That way people who use mem in editors with intellisense will not have to add .default to all their existing require('./mem') calls.
I'm preparing for the future. I'm not interested in rewriting all my type definitions when I move my packages to ES2015 module syntax, so my type definitions assume it's module and I also export a .default to make it ES2015-module-like. I rather optimize the experience for TypeScript and ESM users. My type definitions are not intended for JS users, that's just a bonus that happens to work.
I don't see the problem with having to use .default. You have to do that already if you use any package compiled with Babel from ESM to CommonJS.
With the current designs under consideration require("esm") isn't even going to work (you need to use dynamic import) - so what future is this preparing for?
In chalk you have a .flow definition file that correctly declares the shape of the module as an export assigned thing, but the .d.ts file is likewise incorrect, as in mem. Why have intentionally inaccurate TypeScript metadata, but correct Flow metadata?
When I switch my Node.js modules to ESM, I won't have to inconvenience TypeScript users to update their imports. If I use export = , TS users needs to import with import * as mem from 'mem'; now and later switch to import mem from 'mem'; (unless they use allowSyntheticDefaultImports). In the past, I got many issues of people not understanding why importing didn't work. I have used (faked) default imports for a long time now and you are the first to complain.
With the current designs under consideration require("esm") isn't even going to work (you need to use dynamic import) - so what future is this preparing for?
In chalk you have a .flow definition file that correctly declares the shape of the module as an export assigned thing, but the .d.ts file is likewise incorrect, as in mem. Why have intentionally inaccurate TypeScript metadata, but correct Flow metadata?
I think we were the first to notice the problem because the people most affected by the problem are JS users of VS Code, who don't even know they're using index.d.ts for type information. In JS mode, we don't report errors, so the difference between working and broken is Intellisense vs No Intellisense.
We detected it because @BendingBender is a major contributor to Definitely Typed and we took notice when he started removing packages instead of adding them. :)
I think what @weswigham tries to say here is that the current state of affairs according importing CJS modules from ESM modules is that the whole module.exports definition is made available as a default import. So for example, if we have a CJS module like this:
@sandersn Intellisense interop was not on my radar. I started upstreaming the definitions because it became a burden to keep them up-to-date regarding the pace in which @sindresorhus' modules keep changing.
When I switch my Node.js modules to ESM, I won't have to inconvenience TypeScript users to update their imports. If I use export = , TS users needs to import with import * as mem from 'mem'; now and later switch to import mem from 'mem'; (unless they use allowSyntheticDefaultImports). In the past, I got many issues of people not understanding why importing didn't work. I have used (faked) default imports for a long time now and you are the first to complain.
Having the default in addition to the namespace assignment should be sufficient here. Generally people will need to be swapping from import mem = require('mem'); to import mem from 'mem'; in such a future, and we have a compilation mode (esModuleInterop with the module: "es6" flag set) where only the default import style will end up working, so for users who care about that kind of future compat, the compiler can check it.
But even that has problems, and not with TS. If mem and mem's consumers swap to esm at the same time, you'd be fine... But if a user updates to esm first (or depends on an older mem), then the mem import would be backed by cjs, and instead of getting a callable default you'd get the entire module as the default. So you'd need to say import mem from 'mem'; mem.default() for it to actually work (without a namespace assignment). The default-default is something I hope I don't actually need to see in shipped code, but is ultimately the outcome of a preemtive cjs-namespace-assignment-with-default-but-only-reports-a-default declaration.
When I switch my Node.js modules to ESM, I won't have to inconvenience TypeScript users to update their imports
You're going to inconvenience your TS users less than everyone else, since all your cjs users, under current proposals, will just be completely broken without a reasonable way to use your module. At least your TS users will have the option of running a quick fix (or already be correct) on their import and upgrading their codebase - your cjs users are just dead under the current esm/cjs interop scheme (because replacing require with a promise-returning dynamic import isn't really a practical thing for most places). :(
With the current designs under consideration require("esm") isn't even going to work (you need to use dynamic import) - so what future is this preparing for?
As you said, current designs specify that using
(async()=>{awaitimport('esm');})();
will be possible from CJS modules. Which will import such modules exposing them the way they are authored for ESM, mostly meaning that there will be a default property. Which is the way @sindresorhus' modules are exported in TS.
You can't seriously update any large program using the pattern. For one, is forces you to defer all of your own module's exports because your module now has an async dependency, and you somehow need to report that (eg, by exporting a promise to the real exports namespace), and so the async taint transitively spreads to every dependency. If I were using, eg, pad-left and it updated to esm and required a sweeping change like that to use that would also force me to break all my consumers (since now all my modules need to export promises), I'd just not update.
What I'm getting at here, in the abstract, is that there's no clean migration path in node's esm plan right now. Don't try to craft your declarations for that future, because it doesn't work as well as you think it does, is still in flux, and will only hurt your consumers today.
Only in case of very simple modules that don't have interfaces to export and thus where you can omit the namespace declaration will the compiler enforce the import foo = require('foo') kind of imports.
Or if you set esModuleInterop which was introduced to help close that hole~
@BendingBender Alright. Let's use the correct CommonJS-compatible syntax going forward. I will try to update existing definitions when I have time, but I could use some help with that too.
@sindresorhus I will happily help you with this, most of that stuff comes from me after all. Just for me to know how you plan to change existing declarations: do you want to keep the default export to keep breaking changes for TS users as minimal as possible or do you want to tidy up with this topic?
do you want to keep the default export to keep breaking changes for TS users as minimal as possible or do you want to tidy up with this topic?
Is there a way to move to CommonJS-syntax while still not doing a breaking change? I was under the assumption we had to completely move over and do a major bump.
We could still leave the default export, this would minimize the amount of breaking changes. This isn't clean, however. This default export would have nothing to do with compatibility with ES modules, it just happens to have the same name.
Alright, let's do that for the packages where we already released the type definitions, with a TODO comment about removing it in the next major release.
I didn't read the whole discussion, but I think you just need to add module.exports = mem at the end of the definition file before export default mem. Example:
If you need to add a main.default = main, you must use const main = instead. Currently TS produces a broken definition file if you use a function main. But this export shouldn't be needed. If users want to use a CJS module, they can enable esModuleInterop
Activity
sindresorhus commentedon Mar 18, 2019
I'm preparing for the future. I'm not interested in rewriting all my type definitions when I move my packages to ES2015 module syntax, so my type definitions assume it's module and I also export a
.defaultto make it ES2015-module-like. I rather optimize the experience for TypeScript and ESM users. My type definitions are not intended for JS users, that's just a bonus that happens to work.I don't see the problem with having to use
.default. You have to do that already if you use any package compiled with Babel from ESM to CommonJS.weswigham commentedon Mar 18, 2019
With the current designs under consideration
require("esm")isn't even going to work (you need to use dynamicimport) - so what future is this preparing for?weswigham commentedon Mar 20, 2019
In
chalkyou have a .flow definition file that correctly declares the shape of the module as an export assigned thing, but the .d.ts file is likewise incorrect, as inmem. Why have intentionally inaccurate TypeScript metadata, but correct Flow metadata?weswigham commentedon Mar 20, 2019
I'm unsure how this is different than this issue about how the same problem used to exist in
chalk's flow types, which did get fixed.sindresorhus commentedon Mar 21, 2019
When I switch my Node.js modules to ESM, I won't have to inconvenience TypeScript users to update their imports. If I use
export =, TS users needs to import withimport * as mem from 'mem';now and later switch toimport mem from 'mem';(unless they useallowSyntheticDefaultImports). In the past, I got many issues of people not understanding why importing didn't work. I have used (faked) default imports for a long time now and you are the first to complain.sindresorhus commentedon Mar 21, 2019
The future where I don't use
requireat all.sindresorhus commentedon Mar 21, 2019
I didn't add the Flow declaration.
sindresorhus commentedon Mar 21, 2019
// @BendingBender
sandersn commentedon Mar 21, 2019
I think we were the first to notice the problem because the people most affected by the problem are JS users of VS Code, who don't even know they're using
index.d.tsfor type information. In JS mode, we don't report errors, so the difference between working and broken is Intellisense vs No Intellisense.We detected it because @BendingBender is a major contributor to Definitely Typed and we took notice when he started removing packages instead of adding them. :)
BendingBender commentedon Mar 21, 2019
I think what @weswigham tries to say here is that the current state of affairs according importing CJS modules from ESM modules is that the whole
module.exportsdefinition is made available as a default import. So for example, if we have a CJS module like this:then in an ESM module, this would look like this:
There is, however, a note that named imports should be supported when importing CJS modules from ESM modules.
BendingBender commentedon Mar 21, 2019
@sandersn Intellisense interop was not on my radar. I started upstreaming the definitions because it became a burden to keep them up-to-date regarding the pace in which @sindresorhus' modules keep changing.
weswigham commentedon Mar 21, 2019
Having the default in addition to the namespace assignment should be sufficient here. Generally people will need to be swapping from
import mem = require('mem');toimport mem from 'mem';in such a future, and we have a compilation mode (esModuleInteropwith themodule: "es6"flag set) where only the default import style will end up working, so for users who care about that kind of future compat, the compiler can check it.But even that has problems, and not with TS. If
memandmem's consumers swap to esm at the same time, you'd be fine... But if a user updates to esm first (or depends on an older mem), then thememimport would be backed by cjs, and instead of getting a callable default you'd get the entire module as the default. So you'd need to sayimport mem from 'mem'; mem.default()for it to actually work (without a namespace assignment). The default-default is something I hope I don't actually need to see in shipped code, but is ultimately the outcome of a preemtive cjs-namespace-assignment-with-default-but-only-reports-a-default declaration.You're going to inconvenience your TS users less than everyone else, since all your cjs users, under current proposals, will just be completely broken without a reasonable way to use your module. At least your TS users will have the option of running a quick fix (or already be correct) on their import and upgrading their codebase - your cjs users are just dead under the current esm/cjs interop scheme (because replacing require with a promise-returning dynamic import isn't really a practical thing for most places). :(
BendingBender commentedon Mar 21, 2019
As you said, current designs specify that using
will be possible from CJS modules. Which will import such modules exposing them the way they are authored for ESM, mostly meaning that there will be a
defaultproperty. Which is the way @sindresorhus' modules are exported in TS.weswigham commentedon Mar 21, 2019
You can't seriously update any large program using the pattern. For one, is forces you to defer all of your own module's exports because your module now has an async dependency, and you somehow need to report that (eg, by exporting a promise to the real exports namespace), and so the async taint transitively spreads to every dependency. If I were using, eg,
pad-leftand it updated to esm and required a sweeping change like that to use that would also force me to break all my consumers (since now all my modules need to export promises), I'd just not update.weswigham commentedon Mar 21, 2019
What I'm getting at here, in the abstract, is that there's no clean migration path in node's esm plan right now. Don't try to craft your declarations for that future, because it doesn't work as well as you think it does, is still in flux, and will only hurt your consumers today.
8 remaining items
weswigham commentedon Mar 23, 2019
Or if you set
esModuleInteropwhich was introduced to help close that hole~sindresorhus commentedon Mar 25, 2019
@BendingBender Alright. Let's use the correct CommonJS-compatible syntax going forward. I will try to update existing definitions when I have time, but I could use some help with that too.
BendingBender commentedon Mar 25, 2019
@sindresorhus I will happily help you with this, most of that stuff comes from me after all. Just for me to know how you plan to change existing declarations: do you want to keep the
defaultexport to keep breaking changes for TS users as minimal as possible or do you want to tidy up with this topic?sindresorhus commentedon Mar 25, 2019
Is there a way to move to CommonJS-syntax while still not doing a breaking change? I was under the assumption we had to completely move over and do a major bump.
BendingBender commentedon Mar 25, 2019
We could still leave the
defaultexport, this would minimize the amount of breaking changes. This isn't clean, however. Thisdefaultexport would have nothing to do with compatibility with ES modules, it just happens to have the same name.This would be possible:
sindresorhus commentedon Mar 25, 2019
Alright, let's do that for the packages where we already released the type definitions, with a TODO comment about removing it in the next major release.
fregante commentedon Mar 29, 2019
I didn't read the whole discussion, but I think you just need to add
module.exports = memat the end of the definition file beforeexport default mem. Example:https://github.com/bfred-it/doma/blob/2a5020c9fac45a7b984cc8ce2e1036950a2e72e8/index.ts#L16
BendingBender commentedon Mar 29, 2019
That's correct, but this changes the way how interfaces (and other symbols) have to be exported. Please take a look at the changes in #32.
fregante commentedon Mar 29, 2019
Your PR uses
export = mem, notmodule.exports = mem.export = memrequires the custom/weirdimport mem = require('mem')while my two lines should work for any standardimport/require:BendingBender commentedon Mar 29, 2019
fregante commentedon Mar 29, 2019
You're right.
module.exportsdidn't provide types for TS, but it was just a way for Node to accept my TS modules.I tested your PR and it works fine.
For TS module authors:
TypeScript input
If you need to add a
main.default = main, you must useconst main =instead. Currently TS produces a broken definition file if you use afunction main. But this export shouldn't be needed. If users want to use a CJS module, they can enableesModuleInteropJS output
Definition output
Refactor TypeScript definition to CommonJS compatible export (#20)
TypeScript - Drop faking ESM default export support
Only use a CommonJS export for the TypeScript definition