Skip to content

Overloads for String.prototype.split method #61816

Closed as not planned
Closed as not planned

Description

⚙ Compilation target

ES2020

⚙ Library

lib.es5.d.ts

Missing / Incorrect Definition

It is not wrong definition, but if it is possible, I would like to suggest add overloads for String.prototype.split method

I like to use string template types instead of just string as much as possible, and it works like a charm. The problem is that when I try to split my well-known string, it returns just string[] type which isn't very descriptive

My suggestion is to add simple overloads:

  1. When the separator is string and limit is not provided or undefined
  2. When the separator is string | RegExp and limit is 0

I added zero to the list because it is straightforward to implement that case, but if it means that I also should provide type checking for negative values, noninteger values and correct tuple splitting type then forget about it 😁

We can completely remove limit from overloads. I added it just to get the same type for provided limit as undefined and not provided limit otherwise the return type will be string[] if the value of limit is undefined

Sample Code

type Split<Source extends string, Separator extends string> = Source extends `${infer Left}${Separator}${infer Right}`
    ? [...(string extends Left ? string[] : [`${Left}`]), ...Split<Right, Separator>]
    : Source extends ""
      ? Separator extends ""
          ? []
          : [""]
      : string extends Source
        ? string[]
        : [Source];

interface String {
    split<Source extends string, Separator extends string>(
        this: Source,
        separator: Separator,
        limit?: undefined,
    ): Split<Source, Separator>;

    split(separator: string | RegExp, limit: 0): [];
}

You can see types in action on playground (with examples)

Documentation Link

No response

Activity

  1. MartinJohns commented on Jun 4, 2025

    @MartinJohns
    Contributor

    Duplicate of #56333 / #53362.

  2. EzioMercer commented on Jun 4, 2025

    @EzioMercer
    Author

    Martin Johns (@MartinJohns) Ryan Cavanaugh (@RyanCavanaugh) I don't think that it is a duplicate because in mentioned (and other) issues a lot of cases didn't handled. Please look at playground link which I provided

  3. EzioMercer commented on Jun 5, 2025

    @EzioMercer
    Author

    Ryan Cavanaugh (@RyanCavanaugh) Please give it a chance. I really did my best to write these overloads. I covered a lot of edge cases. I gave about 20 cases that I could think of, but if there are uncovered edge cases that don't work, please feel free to add them in the comments, I'll try to handle them too.

  4. MartinJohns commented on Jun 5, 2025

    @MartinJohns
    Contributor

    It's still a duplicate. Another approach doesn't justify a new issue. You can add your suggestion in the open issue, which is still labelled as "Awaiting More Feedback".

    Also this comment is relevant: #53362 (comment)

    we have a pretty low appetite for making the standard library definitions extremely complex for the sake of NUIA

    Your suggestion is extremely complex for a corner case and requires the addition of a new utility type (potentially causing conflicts with user code). See also No New Utility Types.

  5. EzioMercer commented on Jun 5, 2025

    @EzioMercer
    Author

    Martin Johns (@MartinJohns) I simplified a little my type. I am not a genius of TypeScript so may be someone can come up with less complex definition.

    You can add your suggestion in the open issue

    I will. Thank you for noticing that

    requires the addition of a new utility type

    1. I didn't mean to export this type I thought it can be used "hidden"
    2. The name of this type can be anything like UnsafeSplitTypeOnlyForStringCasesInStringSplitMethod or etc.

    we have a pretty low appetite for making the standard library definitions

    I can't quite understand this. Don't we want to give the user as much information as possible? I understand that it's almost impossible to do everywhere, but why not where it's not that hard?

  6. typescript-bot commented on Jun 8, 2025

    @typescript-bot
    Contributor

    This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  7. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions