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.

Unable to overwrite command with TS overloads

See original GitHub issue

Current 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:open
  • Created 2 years ago
  • Reactions:11
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
edufarrecommented, Mar 1, 2022

Do we have any updates on this? I am using the version 9.5.0 and I am still having this issue when trying to Cypress.Commands.overwrite the original visit command

2reactions
dclowd9901commented, Apr 28, 2022

One 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:

declare namespace Cypress {
  interface Chainable {
    ...
    visit(
      options: Partial<Cypress.VisitOptions & PopulatePreloadsOptions> & { url: string }
    ): Cypress.Chainable<Cypress.AUTWindow>;
    visit(
      url: string,
      options?: Partial<Cypress.VisitOptions & PopulatePreloadsOptions>
    ): Cypress.Chainable<Cypress.AUTWindow>;
    ...
  }
}

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

Read more comments on GitHub >

github_iconTop 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 >

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