-
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
String.replace() : typescript displays wrong message when 2nd argument is a number #13889
Comments
this is a byproduct of the overload resolution process. The compiler does not know what was the "best" match given your arguments, so it reports the error on the last signature. the assumption here is that it is more general. it just happens not to work here. |
Instead of lib.d.ts's current overloads interface String {
replace(searchValue: string, replaceValue: string): string;
replace(searchValue: string, replacer: (substring: string, ...args: any[]) => string): string;
replace(searchValue: RegExp, replaceValue: string): string;
replace(searchValue: RegExp, replacer: (substring: string, ...args: any[]) => string): string;
} it could declare the method using union types: interface String {
replace(searchValue: string | RegExp, replaceValue: string | ((substring: string, ...args: any[]) => string)): string;
} which would give more accurate error messages. |
Precisely, shouldn't Typescript try to find the best match signature instead of pick the last one ? In this example, first argument match one signature, so Typescript could assume that the intended target is that signature, and give an error message related to that one. |
Defining "best" outside of trivial specific examples turns out to be very difficult |
Yes I understand perfectly, but just reporting the error according to the last signature sounds weird no ? 😉 |
Maybe it would be better to create a more generic error when there is a signature mismatch, along the lines of 'No matching signature for In my experience the signature mismatch errors are more confusing than helpful anyway, the way they currently are. |
Yes. While not ideal, it would be better than actual message. |
@RyanCavanaugh should I open another issue for a feature request to ask for a more generic error message ? Or could I just edit the title of this one ? What would you prefer? |
The last signature is picked deliberately. Since overload resolution is order dependent in TS, the last signature is usually more general than previous ones, see I believe the error in this case is not very useful but it is useful in other cases, giving up on it completely is not the correct path. For this specific case, the function definition should be updated to use union types, instead of multiple signatures. this should make the error experience much better. |
Ok, I understand, thx a lot for all your patience & explanation ! |
TypeScript Version: 2.1.5
Code
Expected behavior:
Error on 2nd argument with message "Argument of type Number is not assignable to parameter of type 'String'.
Actual behavior:
Error on 1st argument with message "Argument of type '"a123"' is not assignable to parameter of type 'RegExp'."
Indeed, there's no signature for String.replace() in lib.es6.d.ts with number as 2nd argument, so Typescript should find error on the latter, and not on the first argument, which matches one of the signatures.
The text was updated successfully, but these errors were encountered: