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

Make it possible to import various Cypress types #27973

Open
mgol opened this issue Oct 4, 2023 · 6 comments
Open

Make it possible to import various Cypress types #27973

mgol opened this issue Oct 4, 2023 · 6 comments
Labels
type: feature New feature that does not currently exist type: typings Issue related to Cypress types (for TypeScript)

Comments

@mgol
Copy link

mgol commented Oct 4, 2023

What would you like?

We have a lot of imports like:

import type { Interception as CypressInterceptor } from 'cypress/types/net-stubbing';

in the app.

This used to work fine until I tried to update TypeScript to a version that properly parses the exports field (5.2 but I think all >=5.0 might behave in a similar way) in package.json, which for Cypress contains sub-entries like:

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

Since there's no entry starting with ./types, you can't import from this file like that.

That may be fine but importing directly from cypress doesn't work:

import type { Interception as CypressInterceptor } from 'cypress';

This happens for two reasons. First, types/cypress-npm-api.d.ts declares the cypress module and imports of cypress lead to it. This can be hacked around by importing from cypress/ instead:

import type { Interception as CypressInterceptor } from 'cypress';

but types/index.d.ts is not a module - it does not import or export anything directly, it just has a bunch of pragmas like:

/// <reference path="./net-stubbing.d.ts" />

It seems all the types are supposed to do is to type the built-in globals.

Why is this needed?

A lot of the types Cypress defines are useful for external consumers, e.g. to build custom interceptors, etc. It doesn't seem like there's a way to use the provided types with new TypeScript. Right now, it seems my choices are either hacking the cypress package to export what I need or to redefine all the types in the project - which may be quite daunting as they can get pretty complex.

Other

No response

@mgol
Copy link
Author

mgol commented Oct 4, 2023

To be clear, this still works with "moduleResolution": "node10" (or "node" which is just a deprecated alias of "node10" now) but if you try "node16" or any other modern setting, the exports field is consulted and the above problem appears.

@jennifer-shehane jennifer-shehane added type: feature New feature that does not currently exist type: typings Issue related to Cypress types (for TypeScript) labels Oct 5, 2023
@borislit
Copy link

Any news on this?

@NathanKleekamp
Copy link

Yeah, I'd also like to throw a vote for supporting this sooner rather than later. I would like to start using sub-path exports, but as soon as I turn on moduleResolution: bundler in an application that has Cypress e2e, I start getting Cannot find module 'cypress/types/net-stubbing' or its corresponding type declarations.

@aniketjivtode123
Copy link

Any update or workaround on this ?

@nowinskp
Copy link

nowinskp commented Aug 2, 2024

Any updates or plans for fixing this issue?

@DaveEvans-Thalamos
Copy link

My workaround was to use a relative import directly to the types file in the node_modules folder
import { Method } from "../../node_modules/cypress/types/net-stubbing.js";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature New feature that does not currently exist type: typings Issue related to Cypress types (for TypeScript)
Projects
None yet
Development

No branches or pull requests

7 participants