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

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

Closed
justingrant opened this issue Aug 16, 2021 · 2 comments
Labels
Library definitions Issues or pull requests about core library definitions

Comments

@justingrant
Copy link

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!

@justingrant justingrant added the Library definitions Issues or pull requests about core library definitions label Aug 16, 2021
justingrant added a commit to justingrant/react that referenced this issue Aug 16, 2021
`''.concat(value)` violates current Flow definitons that require args to
concat to be strings only, even though the MDN docs and ES spec allow
args to be any type. See facebook/flow#8728.

To avoid depending on a change to Flow, this commit changes
`''.concat(value)` to `String(value)` and removes the lint rule
that prohibits use of String(value). This rule was added in facebook#9082.
The other rules in that PR (for Boolean and Number constructors)
are not changed in this commit, only the rule for String.

If it's decided that using `concat` is better, simply omit this commit
and use the previous one instead.
@FezVrasta
Copy link
Contributor

There are a lot of instances where Flow requires an actual string rather than letting JS implicitly cast it. I think it's a design decision.

You can always do .concat(String(mixedThing)) to produce the same result.

@justingrant
Copy link
Author

Yep, agree. Per @samwgoldman at facebook/react#22064 (comment), this isn't a change that Flow is going to make, so I'm going to close this issue. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Library definitions Issues or pull requests about core library definitions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants