flow icon indicating copy to clipboard operation
flow copied to clipboard

[PR] Return union void for some string functions

Open Brianzchen opened this issue 5 years ago • 4 comments

Closes #8284

codePointAt and ''[123] has the potential to return undefined. This change will enforce users to put guards around these use cases to provide safer string manipulation.

Brianzchen avatar Aug 30 '20 22:08 Brianzchen

For my index test I had to write it as (new String()[123]: string); because (''[123]: string) wasn't throwing an error. Not sure if I'm missing something here because the previous test that uses string literal works fine.

Brianzchen avatar Aug 30 '20 22:08 Brianzchen

@Brianzchen Looks good, also thanks for the parameter name changes.

I don't really care about flow anymore these days though, sorry.

nnmrts avatar Nov 09 '20 03:11 nnmrts

I've removed the index of check to make this PR a bit easier to get in. Flow team has already expressed that is an area of unsoundness that they are prepared to allow for an easier dev experience.

@nnmrts I understand, hope things get better and you feel interested again soon 😄

Brianzchen avatar Nov 10 '20 05:11 Brianzchen

I think so, the definition if it hasn't been corrected is in fact wrong.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt#return_value

It's documented as

If index is out of the range of 0 – str.length - 1, codePointAt() returns undefined.

Brianzchen avatar Feb 19 '24 22:02 Brianzchen

https://github.com/facebook/flow/commit/ec8a209b46a4635b6a51bdf148a341980fdf3900 would resolve one of the issue. We don't plan to touch the other one, because TS is also being unsound there, and it would be too disruptive.

SamChou19815 avatar Jul 15 '24 23:07 SamChou19815