Skip to content

String split return type #49635

Closed as not planned
Closed as not planned
@Forlini91

Description

@Forlini91

lib Update Request

Configuration Check

My compilation target is ES2020 and my lib is ES2020.

Missing / Incorrect Definition

Method split of String currently has this signature

split(separator: string | RegExp, limit?: number): string[];

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

split(separator: string | RegExp): [string, ...string[]];
split(separator: string | RegExp, limit: number): string[];

Sample Code

Current situation:

const strArr = "123".split("=");  // strArr has type string[]
const item = strArr[0];   // item has type string | undefined

Expected:

const strArr = "123".split("=");  // strArr has type [string, ...string[]]
const item = strArr[0];   // item has type string

I called split without "limit", so I'm sure to find at least 1 element in the returned array

Documentation Link

https://262.ecma-international.org/11.0/#sec-string.prototype.split

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions