Typings for Command.add are missing types for the subject
See original GitHub issueCurrent 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:
- Created 2 years ago
- Reactions:8
- Comments:11
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We have the same problem, typings are not available for child commands since https://github.com/cypress-io/cypress/pull/17496 was introduced.
My workaround does handle all values of prevSubject, just in 2 places rather than one.
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 theAddSubjectArgument
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.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.