Skip to content

Overloads for String.prototype.split method #61827

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4577,3 +4577,19 @@ interface Date {
*/
toLocaleTimeString(locales?: string | string[], options?: Intl.DateTimeFormatOptions): string;
}

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): [];
}
2 changes: 1 addition & 1 deletion tests/baselines/reference/anyAssignableToEveryType.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ var p: Number = a;

var q: String = a;
>q : Symbol(q, Decl(anyAssignableToEveryType.ts, 32, 3))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>a : Symbol(a, Decl(anyAssignableToEveryType.ts, 0, 3))

function foo<T, U /*extends T*/, V extends Date>(x: T, y: U, z: V) {
Expand Down
10 changes: 5 additions & 5 deletions tests/baselines/reference/apparentTypeSubtyping.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Base<U extends String> {
>Base : Symbol(Base, Decl(apparentTypeSubtyping.ts, 0, 0))
>U : Symbol(U, Decl(apparentTypeSubtyping.ts, 3, 11))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

x: U;
>x : Symbol(Base.x, Decl(apparentTypeSubtyping.ts, 3, 30))
Expand All @@ -22,26 +22,26 @@ class Derived<U> extends Base<string> { // error

x: String;
>x : Symbol(Derived.x, Decl(apparentTypeSubtyping.ts, 8, 39))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
}

class Base2 {
>Base2 : Symbol(Base2, Decl(apparentTypeSubtyping.ts, 10, 1))

x: String;
>x : Symbol(Base2.x, Decl(apparentTypeSubtyping.ts, 12, 13))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

static s: String;
>s : Symbol(Base2.s, Decl(apparentTypeSubtyping.ts, 13, 14))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
}

// is U extends String (S) a subtype of String (T)? Apparent type of U is String so it succeeds
class Derived2<U extends String> extends Base2 { // error because of the prototype's not matching, not because of the instance side
>Derived2 : Symbol(Derived2, Decl(apparentTypeSubtyping.ts, 15, 1))
>U : Symbol(U, Decl(apparentTypeSubtyping.ts, 18, 15))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Base2 : Symbol(Base2, Decl(apparentTypeSubtyping.ts, 10, 1))

x: U;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/apparentTypeSupertype.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Base {
class Derived<U extends String> extends Base { // error
>Derived : Symbol(Derived, Decl(apparentTypeSupertype.ts, 5, 1))
>U : Symbol(U, Decl(apparentTypeSupertype.ts, 8, 14))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Base : Symbol(Base, Decl(apparentTypeSupertype.ts, 0, 0))

x: U;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/arrayLiterals2ES5.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ interface myArray2 extends Array<Number|String> { }
>myArray2 : Symbol(myArray2, Decl(arrayLiterals2ES5.ts, 44, 43))
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Number : Symbol(Number, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

var d0 = [1, true, ...temp,]; // has type (string|number|boolean)[]
>d0 : Symbol(d0, Decl(arrayLiterals2ES5.ts, 46, 3))
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/arrayLiterals2ES6.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ interface myArray2 extends Array<Number|String> { }
>myArray2 : Symbol(myArray2, Decl(arrayLiterals2ES6.ts, 42, 43))
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
>Number : Symbol(Number, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --) ... and 1 more)
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --) ... and 2 more)

var d0 = [1, true, ...temp, ]; // has type (string|number|boolean)[]
>d0 : Symbol(d0, Decl(arrayLiterals2ES6.ts, 44, 3))
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/arrayLiterals3.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ interface myArray2 extends Array<Number|String> { }
>myArray2 : Symbol(myArray2, Decl(arrayLiterals3.ts, 29, 43))
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Number : Symbol(Number, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

var c0: tup = [...temp2]; // Error
>c0 : Symbol(c0, Decl(arrayLiterals3.ts, 31, 3))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var x = '';

var a: String;
>a : Symbol(a, Decl(assignFromStringInterface.ts, 1, 3))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

x = a;
>x : Symbol(x, Decl(assignFromStringInterface.ts, 0, 3))
Expand Down
11 changes: 10 additions & 1 deletion tests/baselines/reference/assignFromStringInterface2.errors.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
assignFromStringInterface2.ts(42,1): error TS2322: Type 'NotString' is not assignable to type 'String'.
The types returned by 'split(...)' are incompatible between these types.
Type 'string[]' is not assignable to type '[]'.
Target allows only 0 element(s) but source may have more.
assignFromStringInterface2.ts(47,1): error TS2322: Type 'String' is not assignable to type 'string'.
'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.
assignFromStringInterface2.ts(48,1): error TS2322: Type 'NotString' is not assignable to type 'string'.


==== assignFromStringInterface2.ts (2 errors) ====
==== assignFromStringInterface2.ts (3 errors) ====
interface String {
doStuff(): string;
}
Expand Down Expand Up @@ -46,6 +50,11 @@ assignFromStringInterface2.ts(48,1): error TS2322: Type 'NotString' is not assig

a = x;
a = b;
~
!!! error TS2322: Type 'NotString' is not assignable to type 'String'.
!!! error TS2322: The types returned by 'split(...)' are incompatible between these types.
!!! error TS2322: Type 'string[]' is not assignable to type '[]'.
!!! error TS2322: Target allows only 0 element(s) but source may have more.

b = a;
b = x;
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/assignFromStringInterface2.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

=== assignFromStringInterface2.ts ===
interface String {
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(assignFromStringInterface2.ts, 0, 0))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(assignFromStringInterface2.ts, 0, 0))

doStuff(): string;
>doStuff : Symbol(String.doStuff, Decl(assignFromStringInterface2.ts, 0, 18))
Expand Down Expand Up @@ -145,7 +145,7 @@ var x = '';

var a: String;
>a : Symbol(a, Decl(assignFromStringInterface2.ts, 37, 3))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(assignFromStringInterface2.ts, 0, 0))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(assignFromStringInterface2.ts, 0, 0))

var b: NotString;
>b : Symbol(b, Decl(assignFromStringInterface2.ts, 38, 3))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ y = x;

var a: String = Object.create<Object>("");
>a : Symbol(a, Decl(assigningFromObjectToAnythingElse.ts, 4, 3))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Object.create : Symbol(ObjectConstructor.create, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>create : Symbol(ObjectConstructor.create, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

var c: String = Object.create<Number>(1);
>c : Symbol(c, Decl(assigningFromObjectToAnythingElse.ts, 5, 3))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Object.create : Symbol(ObjectConstructor.create, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>create : Symbol(ObjectConstructor.create, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/bigintIndex.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ typedArray[bigNum] = 0xAA; // should error

typedArray[String(bigNum)] = 0xAA;
>typedArray : Symbol(typedArray, Decl(a.ts, 18, 5))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --) ... and 4 more)
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --) ... and 5 more)
>bigNum : Symbol(bigNum, Decl(a.ts, 17, 5))

typedArray["1"] = 0xBB;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/builtinIterator.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const mapped = iterator.map(String);
>iterator.map : Symbol(IteratorObject.map, Decl(lib.esnext.iterator.d.ts, --, --))
>iterator : Symbol(iterator, Decl(builtinIterator.ts, 0, 5))
>map : Symbol(IteratorObject.map, Decl(lib.esnext.iterator.d.ts, --, --))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --) ... and 7 more)
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --) ... and 8 more)

const filtered = iterator.filter(x => x > 0);
>filtered : Symbol(filtered, Decl(builtinIterator.ts, 4, 5))
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/callOverloads2.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function Goo(s:string); // error - no implementation
declare function Gar(s:String); // expect no error
>Gar : Symbol(Gar, Decl(callOverloads2.ts, 13, 23))
>s : Symbol(s, Decl(callOverloads2.ts, 15, 21))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

var f1 = new Foo("hey");
>f1 : Symbol(f1, Decl(callOverloads2.ts, 17, 3))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class C2 extends Function { }

class C3 extends String { }
>C3 : Symbol(C3, Decl(classExtendingBuiltinType.ts, 1, 29))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>String : Symbol(String, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

class C4 extends Boolean { }
>C4 : Symbol(C4, Decl(classExtendingBuiltinType.ts, 2, 27))
Expand Down
Loading
Loading