Skip to content

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

Closed

Description

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.

Activity

  1. ChiriVulpes commented on Dec 20, 2016

    @ChiriVulpes
    Contributor

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

  2. markboyall commented on Dec 20, 2016

    @markboyall
    Author

    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.

  3. HerringtonDarkholme commented on Dec 20, 2016

    @HerringtonDarkholme
    Contributor

    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
  4. markboyall commented on Dec 20, 2016

    @markboyall
    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.

  5. RyanCavanaugh commented on Dec 20, 2016

    @RyanCavanaugh
    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.

  6. markboyall commented on Dec 21, 2016

    @markboyall
    Author

    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.

  7. added
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    and removed
    Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScript
    SuggestionAn idea for TypeScript
    on Jan 10, 2017
  8. RyanCavanaugh commented on Jan 10, 2017

    @RyanCavanaugh
    Member

    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.

  9. markboyall commented on Jan 10, 2017

    @markboyall
    Author

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

  10. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions