Skip to content

feat: refine .split() return type #56841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/lib/es2015.symbol.wellknown.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ interface PromiseConstructor {
readonly [Symbol.species]: PromiseConstructor;
}

// TODO: inline at usage site
type NonEmptyStringParam<S extends string> = S extends '' ? never : S;
type NonZeroNumberParam<N extends number> = N extends 0 ? never : N;

interface RegExp {
/**
* Matches a string with this regular expression, and returns an array containing the results of
Expand Down Expand Up @@ -207,6 +211,11 @@ interface RegExp {
* @param limit if not undefined, the output array is truncated so that it contains no more
* than 'limit' elements.
*/
[Symbol.split]<S extends string>(string: NonEmptyStringParam<S>): [string, ...string[]];
[Symbol.split]<S extends string, N extends number>(
string: NonEmptyStringParam<S>,
limit: NonZeroNumberParam<N>
): [string, ...string[]];
[Symbol.split](string: string, limit?: number): string[];
}

Expand Down Expand Up @@ -247,6 +256,25 @@ interface String {
* @param splitter An object that can split a string.
* @param limit A value used to limit the number of elements returned in the array.
*/
split(
splitter: {
[Symbol.split]<S extends string>(string: NonEmptyStringParam<S>): [string, ...string[]];
[Symbol.split]<S extends string, NN extends number>(
string: NonEmptyStringParam<S>,
limit: NonZeroNumberParam<NN>
): [string, ...string[]];
}
): [string, ...string[]];
split<N extends number>(
splitter: {
[Symbol.split]<S extends string>(string: NonEmptyStringParam<S>): [string, ...string[]];
[Symbol.split]<S extends string, NN extends number>(
string: NonEmptyStringParam<S>,
limit: NonZeroNumberParam<NN>
): [string, ...string[]];
},
limit: NonZeroNumberParam<N>
): [string, ...string[]];
split(splitter: { [Symbol.split](string: string, limit?: number): string[]; }, limit?: number): string[];
}

Expand Down
5 changes: 5 additions & 0 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,11 @@ interface String {
* @param separator A string that identifies character or characters to use in separating the string. If omitted, a single-element array containing the entire string is returned.
* @param limit A value used to limit the number of elements returned in the array.
*/
split<S extends string>(separator: NonEmptyStringParam<S>): [string, ...string[]];
split<S extends string, N extends number>(
separator: NonEmptyStringParam<S>,
limit: NonZeroNumberParam<N>
): [string, ...string[]];
split(separator: string | RegExp, limit?: number): string[];

/**
Expand Down