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
Fix TypeScript error for CommonJS importing #6695
Conversation
🦋 Changeset detectedLatest commit: bdf385e The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Hmmm. CI failed. so I need to dig into the codebase more. |
I'm not sure about CommonJS details, but the following form seems to work and pass tests: module.exports = stylelint;
module.exports.default = stylelint; In addition, we may need to suppress a TS error like this: // NOTE: For CommonJS compatibility.
// @ts-expect-error -- TS2339: Property 'default' does not exist on type 'PublicApi'.
module.exports.default = stylelint; |
Oops, sorry. I did run the test but I couldn't find the test failure😫 |
Thanks for fixing the error! We can confirm PostCSS also uses the same way: |
I haven't delved deeply into this PR, but is there anything in postcss/postcss#1815 that we need to consider? |
@jeddy3 Thanks for the information. It seems to be the same problem, but hard to find a suitable way for all the situations... 🤔 Does anyone have a nice solution? |
Could you add more context about what postcss/postcss#1815 solves? After read postcss/postcss#1815 , I tried to update "compilerOptions": {
"module": "commonjs",
"moduleResolution": "node16",
}
|
In our case, it seems there are no problems. We can check the Stylelint types on https://arethetypeswrong.github.io/, but no problems are found (this website is introduced on postcss/postcss#1815 (comment)). I have created a small repo to include this PR change: TypeScript compiler (tsc) complains only the following CommonJS code. This code is reported as a problem on #6694, and it should be fixed by this PR. const stylelint = require("stylelint");
console.log(stylelint.utils);
// ~~~~~ TS2339: Property 'utils' does not exist on type 'typeof import("stylelint")'. All the files are executed successfully: $ node index.mjs ; node index.cjs ; node index-default.cjs
{
report: [Function: report],
ruleMessages: [Function: ruleMessages],
validateOptions: [Function: validateOptions],
checkAgainstRule: [Function: checkAgainstRule]
}
{
report: [Function: report],
ruleMessages: [Function: ruleMessages],
validateOptions: [Function: validateOptions],
checkAgainstRule: [Function: checkAgainstRule]
}
{
report: [Function: report],
ruleMessages: [Function: ruleMessages],
validateOptions: [Function: validateOptions],
checkAgainstRule: [Function: checkAgainstRule]
} In short, I think we can merge safely this PR. Any thoughts? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for checking arethetypeswrong for Stylelint
In short, I think we can merge safely this PR. Any thoughts?
I think so too.
Hey, I saw you referenced my PR for postcss, and you made some wrong conclusions. This PR actually makes proper type definitions much uglier / more annoying to maintain by making the same mistake that was made in postcss. Can this be reverted, or at least not be released? It's getting late. I can create a proper PR tomorrow. |
@remcohaszing Thank you for chiming in. We can hold off releasing it. Your input or a PR would be greatly appreciated! |
close: #6694
No.