Unable to overwrite command with TS overloads
See original GitHub issueCurrent behavior
Given I override visit
command as below:
Cypress.Commands.overwrite('visit', (originalFn, url, options) => {
// do something with URL as string
const char = url.includes('?')
return originalFn(url, options);
});
I’m unable to type this:
Cypress.Commands.overwrite<'visit'>('visit', (originalFn, url, options?: Partial<VisitOptions>) => {
// do something with URL as string
const char = url.includes('?') // TS2339: Property 'includes' does not exist on type 'Partial & { url: string; }'
return originalFn(url, options); // TS2554: Expected 1 arguments, but got 2.
});
… as cy.visit
is defined with two type overloads, but only the latter one is taken into account:
visit(url: string, options?: Partial<VisitOptions>): Chainable<AUTWindow>
visit(options: Partial<VisitOptions> & { url: string }): Chainable<AUTWindow>
Desired behavior
I’m able to override visit
command and use visit(url: string, options?: Partial<VisitOptions>): Chainable<AUTWindow>
variant.
Test code to reproduce
Cypress.Commands.overwrite<'visit'>('visit', (originalFn, url, options?: Partial<VisitOptions>) => {
// do something with URL as string
const char = url.includes('?') // TS2339: Property 'includes' does not exist on type 'Partial & { url: string; }'
return originalFn(url, options); // TS2554: Expected 1 arguments, but got 2.
});
Cypress Version
9.2.0
Other
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:11
- Comments:5 (1 by maintainers)
Top Results From Across the Web
TypeScript overloading method on base-type string
It seems like TS functions are invariant on parameter types and f(string, any) can't be saved into a value of type f(string, string)...
Read more >Documentation - TypeScript 4.3
When a method is marked with override , TypeScript will always make sure that a method with the same name exists in a...
Read more >Can We Override Static Method in Java - Javatpoint
In this section, we will understand overloading and overriding in short. We have also explained the answer of why we cannot override the...
Read more >Function Overloading in TypeScript - TutorialsTeacher
Learn how to do function overloading in TypeScript. You can have the multiple functions with the same name but different parameter types and...
Read more >How to bind arguments to handlers in System.CommandLine
CommandLine rejects arguments that don't match these expectations. ... There are overloads of SetHandler that support up to 8 parameters, ...
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
Do we have any updates on this? I am using the version
9.5.0
and I am still having this issue when trying toCypress.Commands.overwrite
the originalvisit
commandOne workaround for this that we used until the Cypress devs release a fix was simply swapping the
visit
function types in our own type definition file:It’s janky, but at least we still get type assertions.
Does anyone know what the canonical way to resolve something like this in Typescript? It seems like it’s probably not solvable using the string literal generic mechanism for
overwrite
. Maybe a predicate?Edit: Looks like it goes back to this question: https://github.com/microsoft/TypeScript/issues/47540