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
This method, when called with a limit argument = 0, returns an empty array, but, when called without limit argument or with a non 0 limit argument, returns an array with at least one element (the initial string).
This means the signature could be changed to acknowledge at least the "missing limit argument" case, where we are sure the array contains at least one element
Ryan Cavanaugh (@RyanCavanaugh)Joshua Chen (@Josh-Cena) I do think there is actually a bug here, if not the one OP is pointing to. Shouldn't the return type be (string | undefined)[] - making the elements of type string | undefined? Seems the undefined case is missed by TS.
It makes it so if a non-empty string literal is provided then the first element is a string, otherwise it behaves as it does today. I don't believe there are any other edge cases where split can return an empty array.
I don't know of a way to differentiate a regular expression literal and a RegExp so that case still always returns an empty first element even though I don't even think it is possible to have an empty regular expression literal (you can do new RegExp('') though).
This compiles with your overload, but doIt("", "") causes an error. In general having a wider input type map to a narrower return type is likely unsound.
So I don't think this can be helped with in a generic way; you just have to assert for each individual use case. Alternatively, if we have negated types, you may be able to do split(sep: string \ "", limit?: number): [string, ...string[]].
Activity
Josh-Cena commentedon Jun 22, 2022
"".split("")RyanCavanaugh commentedon Jun 22, 2022
Yeah, that 👆
helmturner commentedon Jul 8, 2022
Ryan Cavanaugh (@RyanCavanaugh) Joshua Chen (@Josh-Cena) I do think there is actually a bug here, if not the one OP is pointing to. Shouldn't the return type be
(string | undefined)[]- making the elements of typestring | undefined? Seems theundefinedcase is missed by TS.Here is the behavior I'm getting:
Josh-Cena commentedon Jul 9, 2022
AlecVision (@AlecVision) If you want that behavior please turn on
noUncheckedIndexAccess. It's a general problem with index-accessing arrays.Fixes split to return non-null first element in most cases.
MicahZoltu commentedon May 7, 2023
I'm not sure if the team monitors closed issues or if I should open a new issue, but I believe this JS edge case can be handled with:
It makes it so if a non-empty string literal is provided then the first element is a string, otherwise it behaves as it does today. I don't believe there are any other edge cases where split can return an empty array.
I don't know of a way to differentiate a regular expression literal and a
RegExpso that case still always returns an empty first element even though I don't even think it is possible to have an empty regular expression literal (you can donew RegExp('')though).ivanhofer commentedon May 7, 2023
It can return an empty array if you set the limit to
0.In a few applications I'm using the following patch that handles that caswe too:
Josh-Cena commentedon May 7, 2023
Micah Zoltu (@MicahZoltu) Consider the following:
This compiles with your overload, but
doIt("", "")causes an error. In general having a wider input type map to a narrower return type is likely unsound.An empty regex literal is
/(?:)/. (This is what you get when you donew RegExp().toString().) Also, you can get an empty array with regexes as long as the regex is able to match empty strings. For this behavior see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@split. For example:So I don't think this can be helped with in a generic way; you just have to assert for each individual use case. Alternatively, if we have negated types, you may be able to do
split(sep: string \ "", limit?: number): [string, ...string[]].MicahZoltu commentedon May 8, 2023
Ah, you are totally right, I don't know why I thought this would work. 😬