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

Problem with TypeScript signature #477

Closed
mistic100 opened this issue Nov 21, 2023 · 7 comments · Fixed by #478
Closed

Problem with TypeScript signature #477

mistic100 opened this issue Nov 21, 2023 · 7 comments · Fixed by #478
Labels

Comments

@mistic100
Copy link

Hi,

I think there is an error in the return type of the function.

Because

If you return a truthy value, it becomes the subject for the next command.

the function actually returns Chainable<ReturnType> but currently it is declared as Chainable<Subject>

declare namespace Cypress {
  interface Chainable<Subject = any> {
    waitUntil<ReturnType = any>(
      checkFunction: (
        subject: Subject | undefined
      ) => ReturnType | Chainable<ReturnType> | Promise<ReturnType>,
      options?: WaitUntilOptions<Subject>
    ): Chainable<Subject>
  }
}

thanks.

@NoriSte
Copy link
Owner

NoriSte commented Nov 30, 2023

Can you please elaborate more, please? With some eventual IDE's screenshots and/or shiki twoslash reported type? The Chainable<Subject> comes from Cypress and can't be changed

@mistic100
Copy link
Author

mistic100 commented Nov 30, 2023

Your function returns a Chainable object whose value is the one returned by checkFunction and whose type is ReturnType.

Currently the function is typed to return Chainable<Subject> but it should be Chainable<ReturnType>, which is what it actually returns here https://github.com/NoriSte/cypress-wait-until/blob/master/src/index.js#L57 (the initial subject is not returned).

Take this example (written from memory) :

// assume a task "myTask" which return the following type
type MyTaskReturn {
  uid: string;
}

cy.get('#input1')
   // get value of input1
   .its('value')
   // execute the task to make somthing with the value
   .waitUntil((value) => cy.task<MyTaskReturn>('myTask', value))
   // write the task result in another input
   .then(result=> {
      cy.get('#input2').type(result.uid); // ERROR: "uid" is not defined on type string
   });

Here result is typed string because it is the type of the subject passed to waitUntil but it is, in fact, the retuned value of my task, so MyTaskReturn.

I don't see how this couldn't be changed.


I can provide tomorrow a real example, but it is basically this.

@mistic100
Copy link
Author

Real world example : here waitUntil is not chained from another query, as such the result value is not typed at all.

cy.waitUntil(() => {
      return cy.task<string[]>('listFiles', {
          dir: Cypress.config('downloadsFolder'),
          name: name,
      });
  })
  .then((results) => {
     if (results.length === 1) { // ERROR 'results' is possibly undefined
         ....
     }
  });

(the implementation of waitUntil garantees that the result cannot be undefined)

If I force the type with .then((results: string[]) => { it compiltes.

And If I edit your d.ts to return Chainable<ReturnType> it works too.

NoriSte added a commit that referenced this issue Dec 1, 2023
BREAKING CHANGE: TypeScript tests could now throw because of operations made on the value returned
by `checkFunction` (passed to cy.waitUntil). The type was previously `undefined` while now reflects
the type returned by `checkFunction`.

fix #477
NoriSte added a commit that referenced this issue Dec 1, 2023
* chore: update to Node.js 20

* fix: fix waitUntil chained value type

BREAKING CHANGE: TypeScript tests could now throw because of operations made on the value returned
by `checkFunction` (passed to cy.waitUntil). The type was previously `undefined` while now reflecting
the type returned by `checkFunction`.

fix #477
@NoriSte
Copy link
Owner

NoriSte commented Dec 1, 2023

🎉 This issue has been resolved in version 3.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@NoriSte
Copy link
Owner

NoriSte commented Dec 1, 2023

You were right, thank you so much for reporting it ❤️

@NoriSte
Copy link
Owner

NoriSte commented Dec 1, 2023

@all-contributors please add @mistic100 for bug

Copy link
Contributor

@NoriSte

I've put up a pull request to add @mistic100! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants