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
{{ message }}
Repository navigation
Improve string split return type of first array index #53362
Please, consider improving user experience for "".split(...) in cases when string literal is passed, which I believe most users do e.g. split(' ') or split('\n')
Configuration Check
My compilation target is ES2015 and my lib is the default.
And this should added to param JSDoc then: Only passing `""` and `new RegExp("")` values can result in returning an empty array.
Sample Code
This is super inconvenient when used with destructure:
const[firstLine, ...restLine]=str.split('\n')// firstLine is possibly undefined, need add exclamation marks everywhere or `as [string, ...string]` above
I also wanted to do the same for when regexp literal is passed e.g. split(/\n\r?/) as only split(new RegExp('')) can result in empty array (if I'm not mistaken), which I believe no one does, but the literal value can't be captured anyway
FWIW: we have a pretty low appetite for making the standard library definitions extremely complex for the sake of NUIA, which is a flag we added with significant hesitation because it makes things like these difficult to handle.
Hi Vitaly (@zardoy) , Rovshan Badirkhanov (@EzioMercer) , I’d like to work on this issue.
I plan to explore refining the String.prototype.split overloads in lib.es5.d.ts so that destructuring the first element infers string instead of string | undefined when a non-empty separator is used.
I’ll validate the changes with test cases to confirm improved inference and ensure compatibility with existing behavior.
Please let me know if this approach aligns with what the team expects.
Hi Vitaly (@zardoy) , Rovshan Badirkhanov (@EzioMercer) , I’d like to work on this issue.
I plan to explore refining the String.prototype.split overloads in lib.es5.d.ts so that destructuring the first element infers string instead of string | undefined when a non-empty separator is used.
I’ll validate the changes with test cases to confirm improved inference and ensure compatibility with existing behavior.
Please let me know if this approach aligns with what the team expects.
For painful clarity: this suggestion has not been accepted by the TS maintainers and it sounds like they would prefer to avoid doing this. IMHO, noUncheckedIndexedAccess is Technically Correct (The Best Kind Of Correct), and this suggestion falls into the same bucket, so I'd love to see it implemented, but that's not the position of the maintainers.
Ryan Cavanaugh (@RyanCavanaugh) if there is a condition under which this suggestion would be accepted, it would be good to hear that explicitly. If there is no foreseeable case in which the team would want to proceed in this direction, maybe the issue should just be closed? (I'd be sad to see it go but I don't know who it helps to just leave it sitting open in a contested state.)
Activity
RyanCavanaugh commentedon Mar 21, 2023
FWIW: we have a pretty low appetite for making the standard library definitions extremely complex for the sake of NUIA, which is a flag we added with significant hesitation because it makes things like these difficult to handle.
EzioMercer commentedon Jun 5, 2025
Original
I came up with more strict overloads when
limitis0and whenseparatorisstring:You can see types in action on playground (with examples)
zardoy commentedon Jun 5, 2025
Good that we have any activity there, Rovshan Badirkhanov (@EzioMercer) can you open your version of pr?;)
The issue is still really annoying and I wish it could be fixed:(
EzioMercer commentedon Jun 6, 2025
Vitaly (@zardoy) I will try. Why did you dislike my issue?)
kebi-gizachew commentedon Aug 28, 2026
Hi Vitaly (@zardoy) , Rovshan Badirkhanov (@EzioMercer) , I’d like to work on this issue.
I plan to explore refining the
String.prototype.splitoverloads inlib.es5.d.tsso that destructuring the first element infersstringinstead ofstring | undefinedwhen a non-empty separator is used.I’ll validate the changes with test cases to confirm improved inference and ensure compatibility with existing behavior.
Please let me know if this approach aligns with what the team expects.
zardoy commentedon Aug 28, 2026
Reported
kebi-gizachew commentedon Aug 28, 2026
Thanks for confirming. I’ll start working on a PR for this and will share updates once I have test cases ready.
thw0rted commentedon Aug 28, 2026
LOL the bot thinks "Reported" means "sure thing buddy" 😂
thw0rted commentedon Aug 28, 2026
For painful clarity: this suggestion has not been accepted by the TS maintainers and it sounds like they would prefer to avoid doing this. IMHO,
noUncheckedIndexedAccessis Technically Correct (The Best Kind Of Correct), and this suggestion falls into the same bucket, so I'd love to see it implemented, but that's not the position of the maintainers.Ryan Cavanaugh (@RyanCavanaugh) if there is a condition under which this suggestion would be accepted, it would be good to hear that explicitly. If there is no foreseeable case in which the team would want to proceed in this direction, maybe the issue should just be closed? (I'd be sad to see it go but I don't know who it helps to just leave it sitting open in a contested state.)
kebi-gizachew commentedon Aug 28, 2026
Thanks for clarifying, James Bromwell (@thw0rted) — I appreciate the context.