Skip to content

RegExpIndicesArray type definition incompleteΒ #61078

Open
@stefnotch

Description

@stefnotch

πŸ”Ž Search Terms

RegExpIndicesArray, Regex indices

πŸ•— Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play/?target=99#code/DYUwLgBAlgdgJlAxiAzhAvBAFAeiwLwEoAfLAQxKwCNLFCc5CA6EADxESwCIyrEvCAfiawEyFAG4AUFABm2UUlQQAhOkwBXeCFmwQjCAG8pEUxFCQyGaPCUoA2gEYAuhIg4cEezA0BbKiAATgA0ED7+Qc4QZIiIAPaBCDAA5hBgcRAAKgCeAA6oiIFQuWAmZnLkGOoQWnA6egbGZs0Q8TAocaBMwHHJ3Dn5KIXFkChk2WhgABZQaIhkMDBxkFNkufkwAtLNAL4QIMAoIEZlLa1x7Z0g3b3khNtmO1JPQA

πŸ’» 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

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions