microsoft / microsoft/TypeScript
RegExpIndicesArray - Named group indices can be undefined
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- PR merge metrics
- PR metrics pending
Description
### 🔎 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
```ts
const group = /a(?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
https://github.com/microsoft/TypeScript/pull/61079 fixed the same issue for the outer `RegExpIndicesArray` itself, but not the indices array on the matched groups
Contributor guide
Research direction
Start with the linked playground example and compare the group-indices typing with the outer RegExpIndicesArray change in pull request #61079. The work is done when TypeScript reports that the named group's index may be undefined for the shown code, while preserving valid typings for groups that participated in the match.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100