Open
Description
π Search Terms
RegExpIndicesArray, Regex indices
π Version & Regression Information
- This was added in PR Add RegExp indices and hasIndicesΒ #52085
β― Playground Link
π» Code
let indices = (/(z)|(a)|(b)|(c)/d).exec("abc")?.indices;
if (indices !== undefined) {
let a = indices[1]; // [number, number] according to Typescript
// also try out indices[2]. That one has a value.
if(a === undefined) {
console.log("Typescript says this cannot happen");
} else {
console.log(a);
}
}
π Actual behavior
The elements of the RegExpIndicesArray can be undefined, if a capturing group did not match anything at all.
This is demonstrated by running the code above, and by the spec
https://tc39.es/ecma262/multipage/text-processing.html#sec-regexpbuiltinexec
(34. b. ii. Append undefined to indices.)
π Expected behavior
I expected the Typescript type definitions to tell me that the elements of the indices array can be undefined.
Additional information about the issue
Am creating a PR with a fix