ts-reset icon indicating copy to clipboard operation
ts-reset copied to clipboard

improve types for `String.split`

Open ivanhofer opened this issue 1 year ago • 1 comments

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 case string[] makes sense again
  • When the limit is set to 0 an empty array [] gets returned.

ivanhofer avatar Mar 09 '23 07:03 ivanhofer

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.

lo1tuma avatar Sep 23 '23 10:09 lo1tuma

Thanks for the PR! I won't merge it, for reasons given here.

mattpocock avatar May 27 '24 16:05 mattpocock