feat: refine .split() return type #56841
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
NOTE:⚠️ Still work in progress. ⚠️
PR Description
This PR refines
String.split(*)
return type (plus theRegExp
's cousin method).As opposed to previous attempts, it does not try to characterise the "empty array result" as a special case, but quite the opposite, it characterises "
[string, ...string[]]
result" as a special case.With this change, we only get
[string, ...string[]]
as return type when we can statically know for sure that we are not trying to execute''.split('')
, and we do not set the second parameter (limit
) to0
.In other words, the separator must be a literal for this to give as a "nice" return type. If we use a variable, we'll keep the more general
string[]
return type.This (combined with the fact that we can't specialise interfaces for specific literals) means that there are cases where we still get non-empty arrays as a result but our return type won't be able to represent that fact.
Other Relevant Details
I introduced the types
NonEmptyStringParam<>
andNonZeroNumberParam<>
. I suspect that these 2 new types might be not well received. I'm not sure how to make the non-global.Pending tasks:
What this PR tries to fix