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

Fix TypeScript error for CommonJS importing #6695

Merged
merged 6 commits into from Mar 6, 2023

Conversation

baseballyama
Copy link
Contributor

Which issue, if any, is this issue related to?

close: #6694

Is there anything in the PR that needs further explanation?

No.

@changeset-bot
Copy link

changeset-bot bot commented Mar 3, 2023

🦋 Changeset detected

Latest commit: bdf385e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
stylelint Patch

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

@baseballyama
Copy link
Contributor Author

Hmmm. CI failed. so I need to dig into the codebase more.

@ybiquitous ybiquitous changed the title Fix index.d.ts type Fix type definition for CommonJS Mar 3, 2023
@ybiquitous
Copy link
Member

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;

@baseballyama
Copy link
Contributor Author

Oops, sorry. I did run the test but I couldn't find the test failure😫
Now I think we can pass the test.(I run npm run test-coverage locally and all tests passed.)

@ybiquitous ybiquitous changed the title Fix type definition for CommonJS Fix TypeScript error for CommonJS importing Mar 3, 2023
@ybiquitous
Copy link
Member

Thanks for fixing the error! We can confirm PostCSS also uses the same way:

@jeddy3
Copy link
Member

jeddy3 commented Mar 3, 2023

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?

@ybiquitous
Copy link
Member

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

@baseballyama
Copy link
Contributor Author

Could you add more context about what postcss/postcss#1815 solves?

After read postcss/postcss#1815 , I tried to update tsconfig.json as below, still I have a type error even after restarting TS server.
So I couldn't get what issue postcss/postcss#1815 solves with my limited knowledge.😫

  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node16",
  }

image

Property 'utils' does not exist on type 'typeof import("stylelint")'.

@ybiquitous
Copy link
Member

ybiquitous commented Mar 4, 2023

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:
https://github.com/ybiquitous/stylelint-pr-6695

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?

Copy link
Member

@jeddy3 jeddy3 left a 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.

@ybiquitous ybiquitous merged commit a1a1a8a into stylelint:main Mar 6, 2023
@baseballyama baseballyama deleted the patch-1 branch March 6, 2023 00:34
@remcohaszing
Copy link
Contributor

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.

@jeddy3
Copy link
Member

jeddy3 commented Mar 12, 2023

@remcohaszing Thank you for chiming in. We can hold off releasing it. Your input or a PR would be greatly appreciated!

@jeddy3 jeddy3 mentioned this pull request Mar 12, 2023
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Fix type definitions for commonjs
4 participants