Skip to content

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

@kevin-wittmer

Description

@kevin-wittmer

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: typingsIssue related to Cypress types (for TypeScript)

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions