-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Argument of type '() => void' is not assignable to parameter of type '() => Chainable<any>'. #18915
Comments
I have the same issue, but with callback having arguments: Cypress.Commands.add('mockApi', ({ user = {} }) => {
...
}) |
@erwinheitzman I think you need to add the custom method to /// <reference types="cypress" />
declare namespace Cypress {
interface Chainable {
login(): void
}
} See also: https://docs.cypress.io/guides/tooling/typescript-support#Types-for-custom-commands |
That is to get the commands recognised in your tests, however in this case the internal types do not seem correct instead @michaeljaltamirano |
This behavior has been adjusted in #17496 Now you have to add correct types for your custom commands to However, I was under the impression that a custom command does not have to return a Is this assumption incorrect? @remcohaszing If this still holds, the PR is not correct and should be updated. |
I see, seems I misunderstood the changes when reading the PR and changelog. I will try again later today and I'm assuming this will then be resolved. |
I think this is a bug in #17496. I can fix this. Just to make sure before I fix it, it should be like this right? declare global Chainable {
foo(bar: string): Chainable<string>;
}
Cypress.Commands.add('foo', (bar) => {
// $ExpectType string
bar;
// Returning a string is ok
return 'ok';
// Returning a chainable wrapping a string is also ok
return cy.hash();
});
it('should work', () => {
// $ExpectType Chainable<string>
cy.foo('asd');
}); |
Looking at |
Even the example provided fails from https://docs.cypress.io/guides/tooling/typescript-support#Types-for-custom-commands |
@remcohaszing Hi I'm a Cypress guy. The point is we shouldn't be required to return anything from the commands at commands.ts. They don't return any direct value nor can be assigned to variables. Only yields the same that the Cypress command does
Also adding a return before the cy.get command doesn't solve the issue. |
Hey all, for me the issue was resolved after changing the types as suggested by @michaeljaltamirano and @aomader like so: // cypress/support/index.d.ts
declare namespace Cypress {
interface Chainable {
login(): void
}
} If anyone is still having issues, please create an issue specific to your issue as it seems like this can be closed (which I will sometime this weekend if there are no serious objections) so the devs don't have to go through the whole ticket to find what information is relevant and what is not. |
Apparently I cannot close it myself but note to the maintainers, this can be closed. I think it would make sense for people to be able to close their own tickets, might be something to consider. |
Cypress.Commands.add('path', (text: string) => { interface Chainable { while using i am getting this error |
Closing since #18967 should have fixed this. Please open a new issue if this is still broken in the latest Cypress. |
See : cypress-io/cypress#18915 An error 'Argument of type '() => void' is not assignable to parameter of type '() => Chainable<any>' throws when you return directly cypress-io/cypress#18915 (comment) fixes cypress-io#763
same as above comment^^^ |
If anybody else falls here to find out how to get rid of typescript error in declaring commands, you have to wrap your declare global {
namespace Cypress {
interface Chainable {
login(): void;
}
}
} |
Same page here |
This solved it for me
2 create a global.d.ts file that has the the following. Then add the file to tsconfig.json includes property
|
Current behavior
With the upgrade to v9, Cypress is reporting the following error when adding a custom method:
Desired behavior
According to the documentation, no other code should be needed to make this work.
Thus I believe that void should be a valid type to be supported.
Test code to reproduce
Cypress Version
9.0.0
Other
No response
The text was updated successfully, but these errors were encountered: