π Search Terms
RegExpIndicesArray, Regex indices
π Version & Regression Information
- This is the behaviour in every version I tried, and I reviewed the FAQ for entries about Regular Expressions
β― Playground Link
https://www.typescriptlang.org/play/?target=9&ts=6.0.0-dev.20260322#code/PTAEGcCcGNgWwIYEsB2A6ALuAUN6B7FcDUAc0nwFcAHUAXlGAQAoB+AHjiQBMA+AIwCUrWNzQBTAB7jozAOQJocwQEI0qbkmjjwa8lWq6A2gCIu3EwF0A3LgJESGqfTIUaRgAw3sQA
π» Code
const group = /a(?<mid>b)?c/d.exec('ac')!.indices!.groups!["mid"];
const index = group[0];
The typing assumes that group indices are of type [number, number], but these can also be undefined
π Actual behavior
typeof group === "undefined"
hence, the type system should have complained when attempting to access via index.
π Expected behavior
'group' is possibly 'undefined'
...should be reported by the type system.
Additional information about the issue
#61079 fixed the same issue for the outer RegExpIndicesArray itself, but not the indices array on the matched groups
π Search Terms
RegExpIndicesArray, Regex indices
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?target=9&ts=6.0.0-dev.20260322#code/PTAEGcCcGNgWwIYEsB2A6ALuAUN6B7FcDUAc0nwFcAHUAXlGAQAoB+AHjiQBMA+AIwCUrWNzQBTAB7jozAOQJocwQEI0qbkmjjwa8lWq6A2gCIu3EwF0A3LgJESGqfTIUaRgAw3sQA
π» Code
The typing assumes that group indices are of type
[number, number], but these can also beundefinedπ Actual behavior
typeof group === "undefined"hence, the type system should have complained when attempting to access via index.
π Expected behavior
'group' is possibly 'undefined'...should be reported by the type system.
Additional information about the issue
#61079 fixed the same issue for the outer
RegExpIndicesArrayitself, but not the indices array on the matched groups