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
Array.join has improper typing in .d.ts file #13049
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.
IMHOjoin has its unique usage in Array if elements have proper toString method. Joining number, symbol and string is quite common, such as concatenating template code. Forbidding that will be a huge breaking change.
Also, I'm afraid you cannot express a method that can be only used in some specific instance of a class. You might need introduce a new StringArray interface, though.
Update: if you really don't bother.
Switch on noLib in tsconfig.json. Then copy the es5.d.ts in this repo. Change the join signature in Array interface as follow:
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 number[]s. But we can consider it.
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.
Array#join is the same as "" + arr[0] + sep + arr[1] + ... which we allow for any array element type already. It'd be inconsistent to have stricter rules for join than for regular string concatenation.
Activity
ChiriVulpes commentedon Dec 20, 2016
All arrays have the
joinmethod in JS. The type shouldn't reflect what's exposed natively?markboyall commentedon Dec 20, 2016
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.
HerringtonDarkholme commentedon Dec 20, 2016
IMHO
joinhas its unique usage in Array if elements have propertoStringmethod. Joining number, symbol and string is quite common, such as concatenating template code. Forbidding that will be a huge breaking change.Also, I'm afraid you cannot express a method that can be only used in some specific instance of a class. You might need introduce a newStringArrayinterface, though.Update: if you really don't bother.
Switch on
noLibin tsconfig.json. Then copy the es5.d.ts in this repo. Change the join signature in Array interface as follow:markboyall commentedon Dec 20, 2016
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.
RyanCavanaugh commentedon Dec 20, 2016
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
number[]s. But we can consider it.markboyall commentedon Dec 21, 2016
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.
RyanCavanaugh commentedon Jan 10, 2017
Array#joinis the same as"" + arr[0] + sep + arr[1] + ...which we allow for any array element type already. It'd be inconsistent to have stricter rules forjointhan for regular string concatenation.markboyall commentedon Jan 10, 2017
That sounds less like an argument for not fixing join() and more like an argument for also fixing string concatenation.