From fb4c71c8230c76c6105ebf364cc8cba13bcf3d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Correa=20Casablanca?= Date: Thu, 21 Dec 2023 10:45:35 +0100 Subject: [PATCH] feat: refine .split() return type This change still requires to fix the tests. Signed-off-by: Andres Correa Casablanca --- src/lib/es2015.symbol.wellknown.d.ts | 28 ++++++++++++++++++++++++++++ src/lib/es5.d.ts | 5 +++++ 2 files changed, 33 insertions(+) diff --git a/src/lib/es2015.symbol.wellknown.d.ts b/src/lib/es2015.symbol.wellknown.d.ts index cf6ccef52b89f..a25eab0409255 100644 --- a/src/lib/es2015.symbol.wellknown.d.ts +++ b/src/lib/es2015.symbol.wellknown.d.ts @@ -162,6 +162,10 @@ interface PromiseConstructor { readonly [Symbol.species]: PromiseConstructor; } +// TODO: inline at usage site +type NonEmptyStringParam = S extends '' ? never : S; +type NonZeroNumberParam = N extends 0 ? never : N; + interface RegExp { /** * Matches a string with this regular expression, and returns an array containing the results of @@ -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](string: NonEmptyStringParam): [string, ...string[]]; + [Symbol.split]( + string: NonEmptyStringParam, + limit: NonZeroNumberParam + ): [string, ...string[]]; [Symbol.split](string: string, limit?: number): string[]; } @@ -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](string: NonEmptyStringParam): [string, ...string[]]; + [Symbol.split]( + string: NonEmptyStringParam, + limit: NonZeroNumberParam + ): [string, ...string[]]; + } + ): [string, ...string[]]; + split( + splitter: { + [Symbol.split](string: NonEmptyStringParam): [string, ...string[]]; + [Symbol.split]( + string: NonEmptyStringParam, + limit: NonZeroNumberParam + ): [string, ...string[]]; + }, + limit: NonZeroNumberParam + ): [string, ...string[]]; split(splitter: { [Symbol.split](string: string, limit?: number): string[]; }, limit?: number): string[]; } diff --git a/src/lib/es5.d.ts b/src/lib/es5.d.ts index f3df23047022d..4d8d0080d3652 100644 --- a/src/lib/es5.d.ts +++ b/src/lib/es5.d.ts @@ -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(separator: NonEmptyStringParam): [string, ...string[]]; + split( + separator: NonEmptyStringParam, + limit: NonZeroNumberParam + ): [string, ...string[]]; split(separator: string | RegExp, limit?: number): string[]; /**