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

Cypress.Chainable incorectly wrapping any as JQuery<any> in typedef #16669

Closed
MCFreddie777 opened this issue May 25, 2021 · 6 comments · Fixed by #16756
Closed

Cypress.Chainable incorectly wrapping any as JQuery<any> in typedef #16669

MCFreddie777 opened this issue May 25, 2021 · 6 comments · Fixed by #16756
Assignees
Labels
type: typings Issue related to Cypress types (for TypeScript)

Comments

@MCFreddie777
Copy link

MCFreddie777 commented May 25, 2021

Current behavior

When returning any from cypress chainable both following methods return types are Cypress.Chainable<JQuery<any>> which is incorrect.

 foo() {
        return cy.get('body').then((_) => {
            return {} as any;
        });
    }

 foo() {
        return cy.request('/').then(response => {
            return response.body.bar;
        });
    }

image

Desired behavior

Tested on Cypress 6.9.1, it correctly return Cypress.Chainable<any>.

image

Versions

Tested on 6.9.1 - working as expected, 7.3.0, 7.4.0 - incorrect types

@jennifer-shehane jennifer-shehane added the type: typings Issue related to Cypress types (for TypeScript) label May 25, 2021
@jennifer-shehane
Copy link
Member

Maybe related to #15869 or #15624 We're open to a pull request to fix the types.

@jennifer-shehane jennifer-shehane added the stage: ready for work The issue is reproducible and in scope label May 25, 2021
@sainthkh
Copy link
Contributor

sainthkh commented May 31, 2021

Sorry for the late reply.

I checked the code and found out the it failed because the type of response.body is any. It's been raised at #9109 and solved at #16743.

With it solved and you can write code like below:

interface ResBody {
  x: number
  y: string
}

cy.request<ResBody>({
  url: 'http://goooooogle.com',
  body: {}
}).then((resp) => {
  return resp.body.y
}).then(a => {
  a // => string
})

Meanwhile, you can manually cast the type like below:

cy.request({
  url: 'http://goooooogle.com',
  body: {}
}).then((resp) => {
  return resp.body.y as string
}).then(a => {
  a // => string
})

I thought about ways to revert the behavior, but it seems that it's impossible because any extends HTMLElement is true. And we cannot put S extends any on the top, then it'll handle all the definitions.

@sainthkh sainthkh added stage: awaiting response Potential fix was proposed; awaiting response and removed stage: ready for work The issue is reproducible and in scope labels May 31, 2021
@MCFreddie777
Copy link
Author

MCFreddie777 commented May 31, 2021

@sainthkh It's totally okay. Yes I know cy.request is proposed to be generic, but it is not at the moment.

If you look at the first example, it does not relate to the cy.request only, but to the Chainable (.then()) in general.
It casts implicit and explicit any as jQuery<any> which is unwanted behaviour.

image

If you explicitly specify return type of fn, it works okay.

image

But, with other types - it does not.
Here's example with string without explicit function return typedef, it has correct type.

image

@sainthkh
Copy link
Contributor

sainthkh commented Jun 1, 2021

At first, I thought it was impossible because it's how any works. But I found a way.

@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review and removed stage: work in progress labels Jun 1, 2021
@cypress-bot cypress-bot bot added stage: pending release and removed stage: needs review The PR code is done & tested, needs review labels Jun 7, 2021
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jun 7, 2021

The code for this is done in cypress-io/cypress#16756, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jul 20, 2021

Released in 8.0.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v8.0.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Jul 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: typings Issue related to Cypress types (for TypeScript)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants