You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
.call selects the wrong overload for String.prototype.replace #29789
TypeScript Version: v3.3.1 and v3.4.0-dev.20190206
Search Terms: call replace overload
Code
String.prototype.replace.call('one string',/a/g,'two string',// this line errors);
It's worth noting that this is in a JS file, not a TS file, but I'm using allowJs and checkJs.
Expected behavior:
No error.
Actual behavior:
errors with error TS2345: Argument of type '"two string"' is not assignable to parameter of type '(substring: string, ...args: any[]) => string'
The additional overload in lib.es2015.symbol.wellknown needs to be a full union with the regular overload's parameters so that when .call finds it, the regular invocations are still OK.
In the last example, the union of string | ReplacerFn (both of which are valid and work on their own, as demonstrated above) fails as typescript seems to only allow the function variant of the second param to String.replace, giving the error:
Type 'string' is not assignable to type '(substring: string, ...args: any[]) => string'.
Activity
RyanCavanaugh commentedon Feb 14, 2019
The additional overload in lib.es2015.symbol.wellknown needs to be a full union with the regular overload's parameters so that when
.callfinds it, the regular invocations are still OK.jaredh159 commentedon May 20, 2019
I'm pretty sure I've run into this same issue, but not using
.call. This code snippet shows the problem:In the last example, the union of
string | ReplacerFn(both of which are valid and work on their own, as demonstrated above) fails as typescript seems to only allow the function variant of the second param toString.replace, giving the error:Type 'string' is not assignable to type '(substring: string, ...args: any[]) => string'.Playground link:
https://www.typescriptlang.org/play/index.html#src=interface%20ReplacerFn%20%7B%0D%0A%20%20(substr%3A%20string%2C%20...args%3A%20any%5B%5D)%3A%20string%3B%0D%0A%7D%0D%0A%0D%0Afunction%20replaceMeStr(str%3A%20string%2C%20replacer%3A%20string)%20%7B%0D%0A%20%20%20%20return%20str.replace(%2Ffoo%2F%2C%20replacer)%3B%20%2F%2F%20works!%20%3A)%0D%0A%7D%0D%0A%0D%0Afunction%20replaceMeFn(str%3A%20string%2C%20replacer%3A%20ReplacerFn)%20%7B%0D%0A%20%20%20%20return%20str.replace(%2Ffoo%2F%2C%20replacer)%3B%20%2F%2F%20works!%20%3A)%0D%0A%7D%0D%0A%0D%0Afunction%20replaceMe(str%3A%20string%2C%20replacer%3A%20string%20%7C%20ReplacerFn)%20%7B%0D%0A%20%20%20%20return%20str.replace(%2Ffoo%2F%2C%20replacer)%3B%20%2F%2F%20error!%20%3A(%0D%0A%7D%0D%0A%0D%0A
marcin-serwin commentedon Nov 27, 2019
Hi, Could I get any feedback on #34941?