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

Typescript 4.7 with new moduleResolution mode node16/nodenext does not resolve types #22397

Closed
chrisui opened this issue Jun 19, 2022 · 8 comments · Fixed by #22498
Closed

Typescript 4.7 with new moduleResolution mode node16/nodenext does not resolve types #22397

chrisui opened this issue Jun 19, 2022 · 8 comments · Fixed by #22498
Assignees
Labels
CT Issue related to component testing type: bug

Comments

@chrisui
Copy link

chrisui commented Jun 19, 2022

Current behavior

Types are not correctly resolved when using typescript 4.7 with moduleResolution set to "node16".

https://www.typescriptlang.org/docs/handbook/module-resolution.html

error TS2688: Cannot find type definition file for 'cypress'.

Desired behavior

Types should be resolved.

Test code to reproduce

{
  "compilerOptions": {
    "target": "ES2022",
    "lib": ["ES2022"],
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "node16",
    "moduleResolution": "node16",
    "allowJs": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "esModuleInterop": true,
    "types": ["node", "cypress"]
  }
}

Cypress Version

10.1.0

Other

I believe this being due to new module resolution logic requiring explicit extension. Currently the package.json types field just says types (referring to the folder where historically it would've been inferred to use index.d.ts) but now needs to be explicit and configured as types/index.d.ts

@chrisui
Copy link
Author

chrisui commented Jun 19, 2022

Validated that hacking the package.json to be explicit about file appears to solve this issue in my local project.

Ie. In package.json

From

"types": "types",

To

"types": "types/index.d.ts",

@smmccabe
Copy link
Contributor

I get the same problem, but the above fix doesn't work. Also this isn't a problem with cypress v9, just with cypress v10, at least for me. I've been using nodenext since typescript 4.5 as experimental and it has always worked fine with cypress up until now.

@cypress-bot cypress-bot bot added the stage: investigating Someone from Cypress is looking into this label Jun 22, 2022
@chrisui
Copy link
Author

chrisui commented Jun 22, 2022

I may have jumped the gun on my fix that worked - possibly a version mismatch which would validate @smmccabe saying this has been a regression.

@lmiller1990
Copy link
Contributor

Interesting, I will dig into this one tomorrow. One thing we changed from 9.x to 10.x was adding the exports field in package.json, I wonder if this is related?

https://github.com/cypress-io/cypress/blob/develop/cli/package.json#L119-L143

Thanks for the info @smmccabe, I'll verify on my end if it's a regression just to be sure - if so, that narrows the surface area down quite a bit, which is good.

@smmccabe
Copy link
Contributor

You are correct, the exports need to also specify types, see microsoft/TypeScript#33079 and https://devblogs.microsoft.com/typescript/announcing-typescript-4-7-beta/#package-json-exports-imports-and-self-referencing

example:

    ".": {
      "import": "./index.mjs",
      "require": "./index.js",
      "types": "./types/index.d.ts"
    },

I hacked that in and it worked

@cypress-bot cypress-bot bot added stage: routed to ct and removed stage: investigating Someone from Cypress is looking into this stage: routed to ct labels Jun 23, 2022
@mjhenkes mjhenkes added stage: investigating Someone from Cypress is looking into this CT Issue related to component testing type: bug and removed stage: fire watch labels Jun 23, 2022
@lmiller1990
Copy link
Contributor

Nice job debugging this. Would you like to make a PR contributing a fix @smmccabe?

@lmiller1990 lmiller1990 added the stage: awaiting response Potential fix was proposed; awaiting response label Jun 24, 2022
@smmccabe
Copy link
Contributor

already made one just before you posted that :P

@cypress-bot cypress-bot bot added stage: routed to e2e-core and removed stage: investigating Someone from Cypress is looking into this stage: awaiting response Potential fix was proposed; awaiting response labels Jun 24, 2022
@louisscruz
Copy link

I think the blast radius of this issue might be larger than described here. I'm seeing the type issues mentioned above in TypeScript 4.5.2 in a Yarn PnP project. After "unplugging" and applying the fix from #22498, the issue goes away.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CT Issue related to component testing type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants