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
conststring="ABC";string.charAt();string.charCodeAt();string.codePointAt();// ^ no errorsconstmaybeANumber=string.codePointAt(100);Math.round(maybeANumber);// ^ errors because maybeANumber could be undefinedif(typeofmaybeANumber==="number"){Math.round(maybeANumber);// ^ should be fine now because we checked it this time}
Actual behavior
conststring="ABC";string.charAt();string.charCodeAt();string.codePointAt();// ^ errors "Cannot call `string.[...]` because function [1] requires another argument."constmaybeANumber=string.codePointAt(100);Math.round(maybeANumber);// ^ doesn't error but maybeANumber could be undefined
In my opinion, the definitions of these methods are kind of wrong, because
they don't actually require an argument; passing no argument means NaN which means 0 in this context
codePointAt could return undefined when pos is greater than the string's length (ref)
there's no reason for the charAt parameter being called pos instead of index while the others are not (ref)
MDN only calls the codePointAt parameter pos (ref)
TC39 calls all of them pos, but also "index" in their explanations (ref)
we developers probably would all call them index
Point 1 is debatable though, because ECMAScript actually specifies this: String.prototype.charCodeAt ( pos )
and not String.prototype.charCodeAt ( [ pos ] )
which they normally do for actually optional parameters, but in this case, pos will always get converted to NaN if it's undefined anyways. And passing NaN to those methods always returns the first index.
BUT, for example the flow definition of the global function parseInt is technically also not following ECMAScript. Flow says that parseInt accepts mixed as string, but it doesn't. And it says that radix is optional, but it actually isn't. Well, it won't throw, but almost every function of core JavaScript won't throw because of type-juggling and intelligent implementation. I get why Flow allows mixed here, otherwise we would have a pretty hard time checking our strings.
Same goes for isNaN for example. Would be weird if Flow errors on isNaN("test") just because we wanted to check that value, right?
In my opinion, string.charCodeAt() to get the first index should be allowed. It's a common pattern.
Point 2 should be pretty clear.
Point 3 is not that important but I think we should decide on one parameter name for all three methods here.
I like this. I think it adds more protection to a codebase to prevent runtime errors. The same thing happens with 'string'[123] which is defined as always returning a string but should be either string | void which I've seen a few complaints about this before.
For being optional params, I think the current impl is fine because the function is quite useless if you pass nothing in. This can help to ensure you're writing useful code.
Flow version: 0.117.1
Expected behavior
Actual behavior
here
Explanation
In my opinion, the definitions of these methods are kind of wrong, because
NaN
which means 0 in this contextcodePointAt
could returnundefined
whenpos
is greater than the string's length (ref)charAt
parameter being calledpos
instead ofindex
while the others are not (ref)codePointAt
parameterpos
(ref)pos
, but also "index" in their explanations (ref)index
Point 1 is debatable though, because ECMAScript actually specifies this:
String.prototype.charCodeAt ( pos )
and not
String.prototype.charCodeAt ( [ pos ] )
which they normally do for actually optional parameters, but in this case,
pos
will always get converted toNaN
if it's undefined anyways. And passingNaN
to those methods always returns the first index.BUT, for example the flow definition of the global function
parseInt
is technically also not following ECMAScript. Flow says thatparseInt
acceptsmixed
as string, but it doesn't. And it says thatradix
is optional, but it actually isn't. Well, it won't throw, but almost every function of core JavaScript won't throw because of type-juggling and intelligent implementation. I get why Flow allowsmixed
here, otherwise we would have a pretty hard time checking our strings.Same goes for
isNaN
for example. Would be weird if Flow errors onisNaN("test")
just because we wanted to check that value, right?In my opinion,
string.charCodeAt()
to get the first index should be allowed. It's a common pattern.Point 2 should be pretty clear.
Point 3 is not that important but I think we should decide on one parameter name for all three methods here.
So my proposal is that this:
should be this:
or maybe just this:
Thoughts?
The text was updated successfully, but these errors were encountered: