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.

Incorrect method overload resolution for some union types (e.g. in String.prototype.replace)

See original GitHub issue

Bug Report

🔎 Search Terms

string, replace, overload, union types

🕗 Version & Regression Information

It looks like this bug exists in all versions of TypeScript, including nightly, and yes, I’ve checked this:

⏯ Playground Link

💻 Code

// copied straight from String.prototype.replace signature:
export type ReplacerFn = (substring: string, ...args: any[]) => string;
export type Replacer = string | ReplacerFn;

function shouldWork(replacer: Replacer): string {
    return 'Hello, world!'.replace(/anything/, replacer);

function workaroundThatWorks(replacer: Replacer): string {
    if (typeof replacer === 'string') {
        return 'Hello, world!'.replace(/anything/, replacer);
    } else {
        return 'Hello, world!'.replace(/anything/, replacer);

🙁 Actual behavior

TypeScript prints a compilation error for the contents of shouldWork function:

No overload matches this call.
  The last overload gave the following error.
    Argument of type 'Replacer' is not assignable to parameter of type '(substring: string, ...args: any[]) => string'.
      Type 'string' is not assignable to type '(substring: string, ...args: any[]) => string'.(2769)
lib.es5.d.ts(454, 5): The last overload is declared here.

🙂 Expected behavior

I expect that the compiler would recognize that my argument of a custom union type Replacer will always fit one of the existing overloads since String.prototype.replace has them both:

interface String {
    replace(searchValue: { [Symbol.replace](string: string, replaceValue: string): string; }, replaceValue: string): string;
    replace(searchValue: { [Symbol.replace](string: string, replacer: (substring: string, ...args: any[]) => string): string; }, replacer: (substring: string, ...args: any[]) => string): string;

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

fatcerberuscommented, Jul 7, 2021

there is hesitation whether is the game worth a candle or not


RyanCavanaughcommented, Jul 7, 2021

there is hesitation whether is the game worth a candle or not


I thought I’d heard every idiom but apparently not.

Anyway I’m tagging this for discussion because I think we should take another crack at union argument / overload resolution. The 24-combination-limited logic we added for property unions was Good and it seems like we could reuse that approach here to make the vast majority of cases work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

`.call` selects the wrong overload for `String.prototype.replace`
It's worth noting that this is in a JS file, not a TS file, but I'm using allowJs and checkJs . Expected behavior:...
Read more >
TypeScript overloading method on base-type string
Doing "someString".replace("replaceMe", $("row-id")); should work because any can be assigned to string .
Read more >
Documentation - Advanced Types - TypeScript
This page lists some of the more advanced ways in which you can model types, it works in tandem with the Utility Types...
Read more >
Overload resolution -
If a function cannot be selected by overload resolution (e.g. it is a templated entity with a failed constraint), it cannot be named...
Read more >
C++ - Jorge Israel Peña
Overload resolution only considers the functions of a single scope. ... is used as a function's return type, the auto keyword is replaced...
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 Post

No results found

github_iconTop Related Hashnode Post

No results found