question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typings for Command.add are missing types for the subject

See original GitHub issue

Current behavior

A simple custom command that relies on prevSubject is not correctly typed.

Desired behavior

The “previous subject” argument should be correctly added to the command type and error if not passed.

Test code to reproduce

declare global {
  namespace Cypress {
    interface Chainable {
      confirmClick(): void;
    }
  }
}

Cypress.Commands.add('confirmClick', { prevSubject: true }, (confirmButton) => {
  confirmButton.click();
  cy.wrap(confirmButton)
    .should('have.attr', 'data-state', 'confirming')
    .click({ force: true });
});

Cypress Version

9.0.0

Other

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:8
  • Comments:11

github_iconTop GitHub Comments

3reactions
maciaszczykmcommented, Nov 12, 2021

We have the same problem, typings are not available for child commands since https://github.com/cypress-io/cypress/pull/17496 was introduced.

1reaction
SimeonCcommented, Nov 12, 2021

My workaround does handle all values of prevSubject, just in 2 places rather than one.

  fn: Cypress.Chainable[T] extends (...args: any) => any
    ? TOptions['prevSubject'] extends false
      ? Cypress.Chainable[T]
      : AddSubjectArgument<TOptions['prevSubject'], Cypress.Chainable[T]>
    : never

At this place we check if prevSubject is false, which means that the subject should not be added - hence returning Cypress.Chainable[T] otherwise we pass to the AddSubjectArgument type.

Key is this part of the type at this point, our type of TPrevSubject is true | string | string[] (OK, not quite string cause it’s ‘internal’, ‘window’ etc). So the ternary correctly handles between being true and the value cannot be undefined, or the other values where it can be undefined.

...args: [
    TPrevSubject extends true
      ? JQuery | HTMLElement
      : undefined | JQuery | HTMLElement,
    ...Parameters<TFunction>

This is the solution I used in the attached PR which passes all the TS 4 tests, the only problem I have is that this only seems to work in TS 4 so the TS 3 tests fail.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript Typings: The Complete Guide: @types Compiler ...
Missing Node Type Definitions. One way is to try to use the CommonJs require syntax, so let's try that: ...
Read more >
TypeScript typings give me "index.d.ts is not a module"
When I do that RTCPeerConnection is no longer recognized. Adding /// <reference src="node_modules/@types/webrtc/" /> did not help, tsc says Invalid reference ...
Read more >
How to Declare Missing Types for External Libraries -- newline
Custom Types Declaration#. First, in your tsconfig.json add a directory path to type declarations:.
Read more >
TypeScript - Cypress Documentation
When you add a custom command with prevSubject , Cypress will infer the subject type automatically based on the specified prevSubject . //...
Read more >
A quick introduction to “Type Declaration” files and adding ...
A Type Declaration or Type Definition file is a TypeScript file but with .d.ts ... file (or using --declaration flag with the tsc...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found