Incorrect method overload resolution for some union types (e.g. in String.prototype.replace)
See original GitHub issueBug 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: https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs
⏯ 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:
- Created 2 years ago
- Comments:7 (3 by maintainers)
Top 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", $(evt.target).data("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 - cppreference.com
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 >
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 Free
Top 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
what
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.