Skip to content

RegExp#[Symbol.matchAll] should return iterable of RegExpExecArray instead of RegExpMatchArray #60515

Open
@lionel-rowe

Description

@lionel-rowe

🔎 Search Terms

RegExp, matchAll, Symbol.matchAll, RegExpExecArray, RegExpMatchArray

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about RegExp, matchAll, Symbol.matchAll, RegExpExecArray, RegExpMatchArray

⏯ Playground Link

https://www.typescriptlang.org/play/?target=99#code/MYewdgzgLgBATgUxgXhgegHRoOYChSSzRwowDkAhgEbBm64BmIJAFAdDALYwgMzEZOFKMAAWAQQA2klogCUcmAG9cMNejQ8A1jBYIAHgAcEwKAgAm8BBACukqHNXr2sAJYAuGGBucqCEqicGK5g5gbqMLgAvvRMrC5cPHyIANoAygCeviCSgsJiUpIAuizECspOamiaACoZxuTevv4wAD4wNqEIDCEWZDCuEF4gsBQQEK7YYNSSSFAgMFD1SGRNfnBkGJUwCR5ePuukQSFh+tG4QA

💻 Code

const re = /./g
const str = 'abc'

for (const m of str.matchAll(re)) {
    // ok (expected result)
    const i: number = m.index    
}

for (const m of re[Symbol.matchAll](str)) {
    // Type 'number | undefined' is not assignable to type 'number'.
    const i: number = m.index
}

🙁 Actual behavior

While String#matchAll now correctly returns RegExpStringIterator<RegExpExecArray> (#36788, fixed in #55565), the identically-behaving RegExp#[Symbol.matchAll] still returns the incorrect type RegExpStringIterator<RegExpMatchArray>

🙂 Expected behavior

String#matchAll and RegExp#[Symbol.matchAll] to behave identically at compile time as well as runtime, i.e. both String#matchAll and RegExp#[Symbol.matchAll] to consistently return RegExpStringIterator<RegExpExecArray>.

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions