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

Typings for cy.task when chaining with .then #7136

Closed
kevin-wittmer opened this issue Apr 24, 2020 · 3 comments · Fixed by #8753
Closed

Typings for cy.task when chaining with .then #7136

kevin-wittmer opened this issue Apr 24, 2020 · 3 comments · Fixed by #8753
Labels
type: typings Issue related to Cypress types (for TypeScript)

Comments

@kevin-wittmer
Copy link
Contributor

kevin-wittmer commented Apr 24, 2020

Current behavior:

The function passed to .then after calling .task must have its parameter typed as undefined when writing the code in Typescript. Ideally, the function's parameter would match the return type of the task. As a workaround, the function's parameter can be explicitly marked as any then dynamically cast to the desired type, but this goes against Typescript best practices.

Desired behavior:

There are probably a few different ways of accomplishing this, but one that comes to mind is:

// In this example, val would have type MyType
cy.task<MyType>("task").then((val) => ...);

Test code to reproduce

// In this example, val is typed as undefined
cy.task("task").then((val) => ...);

Versions

Cypress 4.4.0

@jennifer-shehane jennifer-shehane added the type: typings Issue related to Cypress types (for TypeScript) label Apr 27, 2020
@cypress-bot cypress-bot bot added the stage: proposal 💡 No work has been done of this issue label Apr 27, 2020
@calummoore
Copy link

In the mean time you can overwrite the type to:

/// <reference types="cypress" />

declare global {
  namespace Cypress {
    interface Chainable<Subject = any> {
      task<T>(event: string, arg?: any, options?: Partial<Loggable & Timeoutable>): Chainable<T>
    }
  }
}

export {}

@cypress-bot cypress-bot bot added stage: work in progress stage: needs review The PR code is done & tested, needs review and removed stage: proposal 💡 No work has been done of this issue stage: work in progress stage: needs review The PR code is done & tested, needs review labels Oct 6, 2020
@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review and removed stage: work in progress labels Oct 13, 2020
@cypress-bot cypress-bot bot added stage: pending release and removed stage: needs review The PR code is done & tested, needs review labels Oct 28, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Oct 28, 2020

The code for this is done in cypress-io/cypress#8753, 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 Nov 9, 2020

Released in 5.6.0.

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

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Nov 9, 2020
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
3 participants