ts-reset
ts-reset copied to clipboard
improve types for `String.split`
This PR improves the type definitions for String.split
. By default it always returns string[]
but depending on the arguments passed, we can infer a few cases e.g.
- Splitting a string will almost always return an array with at least one item, so the type
[string, ...string[]]
makes more sense. - Unless when the string and separator is an empty string
''
, then an empty array[]
is returned. In that casestring[]
makes sense again - When the limit is set to
0
an empty array[]
gets returned.
FWIW: there is also on open issue in the typescript repo, which hasn’t been closed yet. So there is a chance that this will be supported upstream.
That’s being said, I would be very happy about such an improved version of String.prototype.split
. No matter if it is in typescript directly, or here via ts-reset
.
Thanks for the PR! I won't merge it, for reasons given here.