Skip to content
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

String.prototype.matchAll must return RegExpExecArray #55557

Closed
nihil-admirari opened this issue Aug 29, 2023 · 2 comments
Closed

String.prototype.matchAll must return RegExpExecArray #55557

nihil-admirari opened this issue Aug 29, 2023 · 2 comments

Comments

@nihil-admirari
Copy link
Contributor

nihil-admirari commented Aug 29, 2023

⚙ Compilation target

ESNext

⚙ Library

ESNext

Missing / Incorrect Definition

es2020.string.d.ts defines matchAll as returning RegExpMatchArray:

interface String {
    matchAll(regexp: RegExp): IterableIterator<RegExpMatchArray>;
}

RegExpMatchArray has optional index and input:

interface RegExpMatchArray extends Array<string> {
    index?: number;
    input?: string;
    0: string;
}

MDN says that matchAll returns an iterator of matches:

Each match is an array with the same shape as the return value of RegExp.prototype.exec().

But RegExpExecArray's index and input are not optional:

interface RegExpExecArray extends Array<string> {
    index: number;
    input: string;
    0: string;
}

Sample Code

Array.from(str.matchAll(separator)).forEach(m => {
    // doesn't compile with `strictNullChecks`, since `m.index` is `number | undefined`
    const lastIdx = m.index + m[0].length;
});

Documentation Link

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll#return_value

@jakebailey
Copy link
Member

#36788 ?

@nihil-admirari
Copy link
Contributor Author

Oops. Closing as a duplicate.

@nihil-admirari nihil-admirari closed this as not planned Won't fix, can't repro, duplicate, stale Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants