Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegExpIndicesArray type definition incomplete #61078

Open
stefnotch opened this issue Jan 29, 2025 · 3 comments Β· May be fixed by #61079
Open

RegExpIndicesArray type definition incomplete #61078

stefnotch opened this issue Jan 29, 2025 · 3 comments Β· May be fixed by #61079
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@stefnotch
Copy link

πŸ”Ž 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

@jakebailey
Copy link
Member

FWIW this "works" as you expect if you use noUncheckedIndexAccess, but the spec does seem to imply that it can explicitly place undefined in an array, not that it's out of bounds?

@jcalz
Copy link
Contributor

jcalz commented Jan 29, 2025

Like #49228 and issues linked from there? Not considered worth the breakage in those other issues, from the looks of it

@stefnotch
Copy link
Author

stefnotch commented Jan 29, 2025

FWIW this "works" as you expect if you use noUncheckedIndexAccess, but the spec does seem to imply that it can explicitly place undefined in an array, not that it's out of bounds?

Yes, the spec guarantees the behaviour of getting undefined elements in the array. Like [[0, 1], undefined, [0, 1], undefined, undefined] . This is also implemented that way on both Firefox and Chrome.
Even if I were to write seemingly bulletproof code by checking the array length, I would still access undefineds.

noUncheckedIndexAccess does not handle all cases. For example, doing .forEach still results in the same issue.
https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&target=99#code/DYUwLgBAlgdgJlAxiAzhAvBAFAeiwLwEoAfLAQxKwCNLFCc5CA6EADxESwCIyrEvCAfiawEyFAG4AUFABm2UUlQQAhOkwBXeCFmwQjCAG8pEU9HhKUTWQHsATgFEyiABbUMAPgiIbMFDdAmYBsAc2pCQmkzCBMzUEgyDHMxVABtAEYAXQkIHBwIVJgNAFsqEDsAGggi0vLMiGcfOwQYEIgwGwgAFQBPAAdURDsoPrBY0zlyDHUILTgdPQNjaOifPwCQINDuXoGUIZHIFDIetDAXKDREMhgYG0gXMj6BmAEoswBfCBBgFBAjcYrNb+QLBMIUd6mD5SaFAA

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants