Skip to content
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

Closed
markboyall opened this issue Dec 20, 2016 · 8 comments
Closed

Array.join has improper typing in .d.ts file #13049

markboyall opened this issue Dec 20, 2016 · 8 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@markboyall
Copy link

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.

@ChiriVulpes
Copy link
Contributor

All arrays have the join method in JS. The type shouldn't reflect what's exposed natively?

@markboyall
Copy link
Author

markboyall commented 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
Copy link
Contributor

HerringtonDarkholme commented Dec 20, 2016

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:

join(this: string[], sep?: string): string

@markboyall
Copy link
Author

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 RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Dec 20, 2016
@RyanCavanaugh
Copy link
Member

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.

@mhegazy mhegazy added the Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript label Dec 20, 2016
@markboyall
Copy link
Author

markboyall commented 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 RyanCavanaugh added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Jan 10, 2017
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Jan 10, 2017

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.

@markboyall
Copy link
Author

That sounds less like an argument for not fixing join() and more like an argument for also fixing string concatenation.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants