Skip to content

Typescript error while using Cypress.Command.add(): Property 'selector' does not exist on type 'JQuery<HTMLElement>' #20376

@atarek12

Description

@atarek12

Current behavior

I am getting a Typescript error for those cases:

  • Cypress.Command.add() with { prevSubject: true }
Cypress.Commands.add("newCommand", { prevSubject: true }, (element) => {});

In the above example, element is of type unknown but instead, it should be JQuery<HTMLElement>, this could be handled by passing the generics values like that:

Cypress.Commands.add<"newCommand","element">("newCommand", { prevSubject: true }, (element) => {});

But I think passing true to the prevSubject option, the element type should be JQuery<HTMLElement> by itself.

  • Cypress.Command.add() with {prevSubject: "element" }
Cypress.Commands.add("newCommand", { prevSubject: "element" }, (element) => {
  element.selector
});

I am trying to access the selector property, but I got a typescript error that it does not exist on type JQuery<HTMLElement>

Desired behavior

No typescript error should be printed, as the selector property does exist in the element object.

Test code to reproduce

https://github.com/atarek12/cypress/blob/fix-prevSubject-types/cli/types/tests/cypress-tests.ts#L87

Cypress Version

9.5.0

Other

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions