Skip to content

mem intellisense broken for JS #31

Closed

Description

@sandersn

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:

const mem = require('./mem')

isn't callable, even though the primary export of this package is a function.

Instead, people have to change their code to

const mem = 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.

Activity

  1. sindresorhus commented on Mar 18, 2019

    @sindresorhus
    Owner

    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.

  2. weswigham commented on Mar 18, 2019

    @weswigham

    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?

  3. weswigham commented on Mar 20, 2019

    @weswigham

    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?

  4. weswigham commented on Mar 20, 2019

    @weswigham

    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.

  5. sindresorhus commented on Mar 21, 2019

    @sindresorhus
    Owner

    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.

  6. sindresorhus commented on Mar 21, 2019

    @sindresorhus
    Owner

    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?

    The future where I don't use require at all.

  7. sindresorhus commented on Mar 21, 2019

    @sindresorhus
    Owner

    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 didn't add the Flow declaration.

  8. sindresorhus commented on Mar 21, 2019

    @sindresorhus
    Owner
  9. sandersn commented on Mar 21, 2019

    @sandersn
    Author

    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. :)

  10. BendingBender commented on Mar 21, 2019

    @BendingBender
    Contributor

    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:

    const myModule = {prop: 'foo'};
    
    module.exports = myModule;
    module.exports.default = myModule;

    then in an ESM module, this would look like this:

    import myModule from 'my-module';
    
    console.log(myModule.prop);
    // => 'foo'
    console.log(myModule.default.prop);
    // => 'foo'

    There is, however, a note that named imports should be supported when importing CJS modules from ESM modules.

  11. BendingBender commented on Mar 21, 2019

    @BendingBender
    Contributor

    @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.

  12. weswigham commented on Mar 21, 2019

    @weswigham

    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). :(

  13. BendingBender commented on Mar 21, 2019

    @BendingBender
    Contributor

    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 () => {
    	await import('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.

  14. weswigham commented on Mar 21, 2019

    @weswigham

    As you said, current designs specify that using

    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.

  15. weswigham commented on Mar 21, 2019

    @weswigham

    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.

  16. 8 remaining items

  17. weswigham commented on Mar 23, 2019

    @weswigham

    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~

  18. sindresorhus commented on Mar 25, 2019

    @sindresorhus
    Owner

    @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.

  19. BendingBender commented on Mar 25, 2019

    @BendingBender
    Contributor

    @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?

  20. sindresorhus commented on Mar 25, 2019

    @sindresorhus
    Owner

    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.

  21. BendingBender commented on Mar 25, 2019

    @BendingBender
    Contributor

    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.

    This would be possible:

    declare namespace foo {
    	interface Foo {}
    }
    
    declare const foo: {
    	(fooArg: foo.Foo): void;
    	namedExportFn(): void;
    
    	default: typeof foo;
    }
    
    export = foo;
  22. sindresorhus commented on Mar 25, 2019

    @sindresorhus
    Owner

    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.

  23. fregante commented on Mar 29, 2019

    @fregante
    Collaborator

    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:

    https://github.com/bfred-it/doma/blob/2a5020c9fac45a7b984cc8ce2e1036950a2e72e8/index.ts#L16

  24. BendingBender commented on Mar 29, 2019

    @BendingBender
    Contributor

    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.

  25. fregante commented on Mar 29, 2019

    @fregante
    Collaborator

    Your PR uses export = mem, not module.exports = mem.

    export = mem requires the custom/weird import mem = require('mem') while my two lines should work for any standard import/require:

    module.exports = mem;
    export default mem;
  26. BendingBender commented on Mar 29, 2019

    @BendingBender
    Contributor
  27. fregante commented on Mar 29, 2019

    @fregante
    Collaborator

    You're right. module.exports didn'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

    function main (): void {
    }
    
    main.sub = (): void => {
    }
    
    export = main;

    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

    JS output

    "use strict";
    function main() {
    }
    main.sub = () => {
    };
    module.exports = main;
    //# sourceMappingURL=index.js.map

    Definition output

    declare function main(): void;
    declare namespace main {
        var sub: () => void;
    }
    export = main;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions