Skip to content

String.prototype.concat() should accept Array<mixed>, not only Array<string> #8728

@justingrant

Description

@justingrant

Missing/Incorrect APIs

String.prototype.concat should allow any type as its arguments.

concat(...values: Array<string>): string should be concat(...values: Array<mixed>): string

Relevant documentation

According to the MDN docs for the concat() method of the String built-in type:

If the arguments are not of the type string, they are converted to string values before concatenating.

Passing numbers, objects, etc to String.prototype.concat is is valid JS behavior but causes an error in Flow.

This is important because the alternative to String#concat is the + operator, but that operator is problematic because using + to concatenate a string to an object will cause valueOf to be called on the object before it's coerced to a string. Some types (notably the new ECMAScript "Temporal" date/time API that's currently in Stage 3) will throw when valueOf is called so that users don't inadvertently use < or > to compare objects that have specialized comparison methods.

BTW, I discovered this Flow problem while fixing React's use of the + operator on strings and objects, because React was crashing and/or displaying confusing error messages when Temporal instances were rendered or used as props. This issue is causing facebook/react#22064 to fail its Flow check.

PR coming!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Library definitionsIssues or pull requests about core library definitions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions