microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.26k stars 12.52k forks source link

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

Open lionel-rowe opened 1 week ago

lionel-rowe commented 1 week ago

๐Ÿ”Ž Search Terms

RegExp, matchAll, Symbol.matchAll, RegExpExecArray, RegExpMatchArray

๐Ÿ•— Version & Regression Information

โฏ 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> (https://github.com/microsoft/TypeScript/issues/36788, fixed in https://github.com/microsoft/TypeScript/pull/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