This is the behavior in every version I tried, and I reviewed the FAQ for entries about RegExp, matchAll, Symbol.matchAll, RegExpExecArray, RegExpMatchArray
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
}
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>.
๐ Search Terms
RegExp, matchAll, Symbol.matchAll, RegExpExecArray, RegExpMatchArray
๐ Version & Regression Information
โฏ Playground Link
https://www.typescriptlang.org/play/?target=99#code/MYewdgzgLgBATgUxgXhgegHRoOYChSSzRwowDkAhgEbBm64BmIJAFAdDALYwgMzEZOFKMAAWAQQA2klogCUcmAG9cMNejQ8A1jBYIAHgAcEwKAgAm8BBACukqHNXr2sAJYAuGGBucqCEqicGK5g5gbqMLgAvvRMrC5cPHyIANoAygCeviCSgsJiUpIAuizECspOamiaACoZxuTevv4wAD4wNqEIDCEWZDCuEF4gsBQQEK7YYNSSSFAgMFD1SGRNfnBkGJUwCR5ePuukQSFh+tG4QA
๐ป Code
๐ Actual behavior
While
String#matchAll
now correctly returnsRegExpStringIterator<RegExpExecArray>
(https://github.com/microsoft/TypeScript/issues/36788, fixed in https://github.com/microsoft/TypeScript/pull/55565), the identically-behavingRegExp#[Symbol.matchAll]
still returns the incorrect typeRegExpStringIterator<RegExpMatchArray>
๐ Expected behavior
String#matchAll
andRegExp#[Symbol.matchAll]
to behave identically at compile time as well as runtime, i.e. bothString#matchAll
andRegExp#[Symbol.matchAll]
to consistently returnRegExpStringIterator<RegExpExecArray>
.Additional information about the issue
No response