Skip to content

Add types for String.{matchAll,replaceAll} with a well known symbol #61449

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 2 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
1 change: 1 addition & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ const libEntries: [string, string][] = [
["es2020.intl", "lib.es2020.intl.d.ts"],
["es2020.number", "lib.es2020.number.d.ts"],
["es2021.promise", "lib.es2021.promise.d.ts"],
["es2021.symbol.wellknown", "lib.es2021.symbol.wellknown.d.ts"],
["es2021.string", "lib.es2021.string.d.ts"],
["es2021.weakref", "lib.es2021.weakref.d.ts"],
["es2021.intl", "lib.es2021.intl.d.ts"],
Expand Down
9 changes: 9 additions & 0 deletions src/lib/es2020.symbol.wellknown.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ interface RegExp {
*/
[Symbol.matchAll](str: string): RegExpStringIterator<RegExpMatchArray>;
}

interface String {
/**
* Matches a string or an object that supports being matched against, and
* returns an iterable of matches containing the results of that search.
* @param regexp An object that supports being matched against.
*/
matchAll(matcher: { [Symbol.matchAll](str: string): RegExpStringIterator<RegExpMatchArray>; }): RegExpStringIterator<RegExpExecArray>;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String.prototype.matchAll, when on the [Symbol.matchAll] codepath, just forwards the call to the passed matcher and returns whatever it returns. There's no limitations on the type of the return and no coercion done. Should this then be generic in the return type?

matchAll<T>(matcher: { [Symbol.matchAll](str: string): T; }): T;

}
1 change: 1 addition & 0 deletions src/lib/es2021.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference lib="es2020" />
/// <reference lib="es2021.promise" />
/// <reference lib="es2021.string" />
/// <reference lib="es2021.symbol.wellknown" />
/// <reference lib="es2021.weakref" />
/// <reference lib="es2021.intl" />
6 changes: 4 additions & 2 deletions src/lib/es2021.string.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/// <reference lib="es2021.symbol.wellknown" />

interface String {
/**
* Replace all instances of a substring in a string, using a regular expression or search string.
* @param searchValue A string to search for.
* @param searchValue An object that supports searching for and replacing matches within a string.
* @param replaceValue A string containing the text to replace for every successful match of searchValue in this string.
*/
replaceAll(searchValue: string | RegExp, replaceValue: string): string;

/**
* Replace all instances of a substring in a string, using a regular expression or search string.
* @param searchValue A string to search for.
* @param searchValue An object that supports searching for and replacing matches within a string.
* @param replacer A function that returns the replacement text.
*/
replaceAll(searchValue: string | RegExp, replacer: (substring: string, ...args: any[]) => string): string;
Expand Down
17 changes: 17 additions & 0 deletions src/lib/es2021.symbol.wellknown.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference lib="es2015.symbol.wellknown" />

interface String {
/**
* Replace all instances of a substring in a string, using an object that supports replacement within a string.
* @param searchValue A string to search for.
* @param replaceValue A string containing the text to replace for every successful match of searchValue in this string.
*/
replaceAll(searchValue: { [Symbol.replace](string: string, replaceValue: string): string; }, replaceValue: string): string;

/**
* Replace all instances of a substring in a string, using an object that supports replacement within a string.
* @param searchValue A string to search for.
* @param replacer A function that returns the replacement text.
*/
replaceAll(searchValue: { [Symbol.replace](string: string, replacer: (substring: string, ...args: any[]) => string): string; }, replacer: (substring: string, ...args: any[]) => string): string;
}
1 change: 1 addition & 0 deletions src/lib/libs.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"es2020.symbol.wellknown",
"es2020.intl",
"es2020.number",
"es2021.symbol.wellknown",
"es2021.string",
"es2021.promise",
"es2021.weakref",
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.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.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.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --) ... and 9 more)

const filtered = iterator.filter(x => x > 0);
>filtered : Symbol(filtered, Decl(builtinIterator.ts, 4, 5))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,19 @@
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-es2021/string' was not resolved. ========",
"======== Resolving module '@typescript/lib-es2021/symbol-wellknown' from '/.src/__lib_node_modules_lookup_lib.es2021.symbol.wellknown.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-es2021/symbol-wellknown' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-es2021/symbol-wellknown'",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-es2021/symbol-wellknown'",
"Loading module '@typescript/lib-es2021/symbol-wellknown' from 'node_modules' folder, target file types: JavaScript.",
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-es2021/symbol-wellknown' was not resolved. ========",
"======== Resolving module '@typescript/lib-es2021/weakref' from '/.src/__lib_node_modules_lookup_lib.es2021.weakref.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-es2021/weakref' from 'node_modules' folder, target file types: TypeScript, Declaration.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,21 @@
"======== Module name '@typescript/lib-es2021/string' was not resolved. ========",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"======== Resolving module '@typescript/lib-es2021/symbol-wellknown' from '/.src/__lib_node_modules_lookup_lib.es2021.symbol.wellknown.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-es2021/symbol-wellknown' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-es2021/symbol-wellknown'",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-es2021/symbol-wellknown'",
"Loading module '@typescript/lib-es2021/symbol-wellknown' from 'node_modules' folder, target file types: JavaScript.",
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-es2021/symbol-wellknown' was not resolved. ========",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"======== Resolving module '@typescript/lib-es2021/weakref' from '/.src/__lib_node_modules_lookup_lib.es2021.weakref.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-es2021/weakref' from 'node_modules' folder, target file types: TypeScript, Declaration.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,19 @@
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-es2021/string' was not resolved. ========",
"======== Resolving module '@typescript/lib-es2021/symbol-wellknown' from '/.src/__lib_node_modules_lookup_lib.es2021.symbol.wellknown.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-es2021/symbol-wellknown' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-es2021/symbol-wellknown'",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-es2021/symbol-wellknown'",
"Loading module '@typescript/lib-es2021/symbol-wellknown' from 'node_modules' folder, target file types: JavaScript.",
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-es2021/symbol-wellknown' was not resolved. ========",
"======== Resolving module '@typescript/lib-es2021/weakref' from '/.src/__lib_node_modules_lookup_lib.es2021.weakref.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-es2021/weakref' from 'node_modules' folder, target file types: TypeScript, Declaration.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,21 @@
"======== Module name '@typescript/lib-es2021/string' was not resolved. ========",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"======== Resolving module '@typescript/lib-es2021/symbol-wellknown' from '/.src/__lib_node_modules_lookup_lib.es2021.symbol.wellknown.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-es2021/symbol-wellknown' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-es2021/symbol-wellknown'",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'typescript__lib-es2021/symbol-wellknown'",
"Loading module '@typescript/lib-es2021/symbol-wellknown' from 'node_modules' folder, target file types: JavaScript.",
"Searching all ancestor node_modules directories for fallback extensions: JavaScript.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@typescript/lib-es2021/symbol-wellknown' was not resolved. ========",
"File '/.ts/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"======== Resolving module '@typescript/lib-es2021/weakref' from '/.src/__lib_node_modules_lookup_lib.es2021.weakref.d.ts__.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module '@typescript/lib-es2021/weakref' from 'node_modules' folder, target file types: TypeScript, Declaration.",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading