cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.66k stars 3.16k forks source link

cy.wrap and .then have incorrect type without having "dom" in tsconfig "lib" #28742

Closed bvandercar-vt closed 1 month ago

bvandercar-vt commented 7 months ago

Current behavior

If not including "dom" in "lib" in tsconfig, i.e. "lib": ["ESNext", "DOM"], a ts error occurs in the following, but shouldn't:

cy.wrap

const getChainable: () => Cypress.Chainable<string> = () => cy.wrap('a')
Type 'Chainable<JQuery<string>>' is not assignable to type 'Chainable<string>'.
  Types of property 'and' are incompatible.
    Type 'Chainer<JQuery<string>>' is not assignable to type 'Chainer<string>'.
      Types of parameters 'value' and 'value' are incompatible.
        Type 'string' is not assignable to type 'JQuery<string>'.ts(2322)

(method) Cypress.Chainable<undefined>.wrap<string>(element: string | JQuery<string>, options?: Partial<Cypress.Loggable & Cypress.Timeoutable> | undefined): Cypress.Chainable<JQuery<string>> (+2 overloads)

In other words, the problem is that it assumes the incorrect overload from the Cypress types file. It should assume this use:

wrap<S>(object: S, options?: Partial<Loggable & Timeoutable>): Chainable<S>

but it's assuming this use:

wrap<E extends Node = HTMLElement>(element: E | JQuery<E>, options?: Partial<Loggable & Timeoutable>): Chainable<JQuery<E>>

If "dom" is added to "lib", no error, the correct use is inferred.

cy.then

declare global {
  namespace Cypress {
    interface Chainable {
      apiPost<T = Record<string, any>>(
        endpoint: string,
        body: Record<string, any>
      ): Chainable<Response<T>>
    }
  }
}

Cypress.Commands.add(
  'apiPost',
  (endpoint: string, body: Record<string, any>) =>
    cy
      .request({
        method: 'POST',
        url: endpoint,
        body,
      })
      .then((response) => response)
)
Argument of type '(endpoint: string, body: Record<string, any>) => Cypress.Chainable<JQuery<Cypress.Response<any>>>' is not assignable to parameter of type 'CommandFn<"apiPost">'.
  Type 'Chainable<JQuery<Response<any>>>' is not assignable to type 'void | Chainable<Response<unknown>>'.
    Type 'Chainable<JQuery<Response<any>>>' is not assignable to type 'Chainable<Response<unknown>>'.
      Types of property 'and' are incompatible.
        Type 'Chainer<JQuery<Response<any>>>' is not assignable to type 'Chainer<Response<unknown>>'.
          Types of parameters 'value' and 'value' are incompatible.
            Type 'Response<unknown>' is missing the following properties from type 'JQuery<Response<any>>': jquery, length, add, addBack, and 134 more.ts(2345)

Desired behavior

Infer the correct types without having to add "dom" to tsconfig lib

Test code to reproduce

In description above.

Cypress Version

13.5.0

Node version

18.15.0

Operating System

Windows 11

Debug Logs

No response

Other

No response

cypress-app-bot commented 1 month ago

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

cypress-app-bot commented 1 month ago

This issue has been closed due to inactivity.