-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Incorrect method overload resolution for some union types (e.g. in String.prototype.replace) #44919
Comments
See #40827 and linked issues. A union type does not work for overload resolution. |
The overload resolution is correct, but maybe the replace method shouldn't be overloaded at all. |
@MartinJohns, could the idea of @ilogico become a solution to my specific case, i.e., edit |
That decision would be up to the TypeScript team. I can't think of a reason why this would be a breaking change, but the documentation would definitely suffer from it. |
Okay, so I'll summarize what I've understood so far. This issue is a duplicate, and it belongs to a
Therefore, the only current way to fix this issue would be to convert |
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. |
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: https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs
⏯ Playground Link
https://www.typescriptlang.org/play?ts=4.4.0-beta#code/PTAEGMHsAcEsFMAmoDOAXATgQ1gcwBZqgBmGkAtqAMqawB2uAdNGWpGgJ7TyMbzQAbLOHio8dLGgCufAFwAoeAA9okDEU7dQAJX5CRGAGJ1QAXlAAKFFIBG6DPVyzUtBgBpQjL1gy4UzrDoOAG0AXQBKMwA+FwcGAG5FFTUNLlFdQWF4DDNYx1AAHx09LKM6RPliKTpwNFhIExR8SCkBRAB1NQBrCz5Mg2cM-Wzw53t8gG95UBnQPmkMEwByAAl4AQFIDwB3NTaAQiXeEpELYECONHxHYA8+4YxwxIBfeUrq2vqTXYwunxa6IgACr4SSdX4oXonbKDaGPMauXCgKazUCwYiWTTwSAY+6lMymcxLcYMJaRFGo2bzGTLNYbLagH4HI5407nIJXG53OFPaazZ6gdYoUQUylzeALWnrTY7PaIQ7HfrwM4XTkMW7ipWPRKo16vIA
💻 Code
🙁 Actual behavior
TypeScript prints a compilation error for the contents of
shouldWork
function:🙂 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 sinceString.prototype.replace
has them both:The text was updated successfully, but these errors were encountered: