Skip to content
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

Package does not pass AreTheTypesWrong #70

Closed
jakebailey opened this issue Dec 2, 2024 · 9 comments
Closed

Package does not pass AreTheTypesWrong #70

jakebailey opened this issue Dec 2, 2024 · 9 comments

Comments

@jakebailey
Copy link

See: https://arethetypeswrong.github.io/?p=eslint-plugin-react-refresh%400.4.16

The package is bundled, and ends with:

var src_default = { rules, configs };
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  configs,
  rules
});

This means that the output dts file should actually read export = _default;, not export default _default;, since an export named default does not actually exist at runtime.

I noticed this on DT, which previously held types for this package (but will have to be deleted now that you're doing it): https://github.com/DefinitelyTyped/DefinitelyTyped/actions/runs/12118993180/job/33784693075#step:12:2663

@jakebailey
Copy link
Author

jakebailey commented Dec 2, 2024

Er, sorry, misread esbuild's output.

var src_exports = {};
__export(src_exports, {
  configs: () => configs,
  default: () => src_default,
  rules: () => rules
});
module.exports = __toCommonJS(src_exports);

I think it actually needs export = _default and export { _default as default }?

@jakebailey
Copy link
Author

jakebailey commented Dec 2, 2024

I should really just defer to the ATTW message 😅

The JavaScript appears to set both module.exports and module.exports.default for improved compatibility, but the types only reflect the latter (by using export default). This will cause TypeScript under the node16 module mode to think an extra .default property access is required, which will work at runtime but is not necessary. These types should export = an object with a default property instead of using export default.

@ArnaudBarre
Copy link
Owner

Oh I didn't know someone published types for this package.
I'll update the suggested syntax

@jakebailey
Copy link
Author

My suggestion is probably wrong; you may need to declare a different type instead that's an object type with a default prop and not mix syntax. I think TS will error?

@ArnaudBarre
Copy link
Owner

Finally took some time to do the change suggested by ATTW cli, it was green after the change.

@jakebailey
Copy link
Author

I'm not sure that change is exactly correct, though it does pass ATTW; the runtime code impies that code like:

const plugin = require("eslint-plugin-react-refresh");

plugin.configs;
plugin.rules;

Will work, but the new types imply that the exported object only has a prop named default.

image

@jakebailey
Copy link
Author

(I had forgotten about this; when I have some time later I can try and come up with a "real" declaration for the shape of your runtime export.)

@ArnaudBarre
Copy link
Owner

export = _default both is valid for the cli and provide correct typing for moduleResolution bundler and node16 (with allowSyntheticDefaultImports).

I think this is good enough for me!

@jakebailey
Copy link
Author

I'm testing with node16 in a plain CJS file, where those synthetic defaults and such don't work since TS wouldn't transform them (which should be the case for nearly all eslint configs).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants