-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Array.join has improper typing in .d.ts file #13049
Comments
All arrays have the |
The output is utterly meaningless in every respect, so no, it certainly should not. The whole point of TypeScript is that what's exposed natively in JS is often bad and wrong. |
IMHO
Update: if you really don't bother. Switch on join(this: string[], sep?: string): string |
I considered doing that, but we thought that it was not worth having to maintain our own copies of lib.d.ts to fix this issue. Since there is absolutely no way to determine if the toString() method is proper or not, forbidding it seems like the only safe way to go. |
This would be a fairly substantial breaking change, and I think you're going to get a lot of people who object to disallowing this on |
I'm more concerned about forbidding it for user-defined random interfaces and class types. Permitting it on, say, (string | number)[] would be fine. Might be better to say it should be allowed for interfaces that explicitly declare/define a toString() method, then just say that string, number count as explicit definitions. |
|
That sounds less like an argument for not fixing join() and more like an argument for also fixing string concatenation. |
To reproduce:
class X {}
let arr: X[] = [];
arr.join("");
Expected behaviour: compiler error.
Actual behaviour: compiler accepts.
The array.join function should only be available for arrays of strings.
The text was updated successfully, but these errors were encountered: