-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Property 'groups' does not exist on type 'RegExprMatchArray' for named captured groups #29465
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
Comments
You should set target to es2018 or higher (for example esnext) or add Example 1 (target to es2018 or higher): {
"compilerOptions": {
"target": "es2018"
}
} Example 2 (using lib config): {
"compilerOptions": {
"target": "es2015",
"lib": [ "es2015", "es2018.regexp" ]
}
} Example 3 (adding reference lib on code): {
"compilerOptions": {
"target": "es2015" // probably can be lower
}
}
/// <reference lib="es2018.regexp" />
console.log("john_doe".match(/^(?<first>[a-z]+).+$/).groups.first) // john
console.log((<any>"john_doe".match(/^(?<first>[a-z]+).+$/)).groups.first) // john Note: I tested all of this with version 3.3.0-dev.20190118. |
Initially thought that it must be it but the error still persists even after making two attempts based on your suggestions. Mind replicating the solution on stackblitz, here's the link |
stackblitz is running an older version of TS that doesn't even understand the |
I still got this error in the latest 3.7.3, see more on #35604. |
Happens in the Typescript sandbox (target set to ES2020, Module ESNext). |
Mismatch still happens even with compiler target and lib set to ESNext, ES2018 etc. const ip = "127.0.0.1";
const ipRegex = new RegExp(/(?<firstOctet>\d{1,3})\.(?<secondOctet>\d{1,3})\.(?<thirdOctet>\d{1,3})\.(?<fourthOctet>\d{1,3})/);
if(!ipRegex.test(ip)) {
return "UNRECOGNIZED_FORMAT";
}
const { groups } = ipRegex.exec(ip);
console.log(groups.firstOctet);
console.log(groups.secondOctet);
console.log(groups.thirdOctet);
console.log(groups.fourthOctet); |
@miccies3 Works fine. Playground link |
TypeScript Version: 3.3.0-dev.201xxxxx
Search Terms:
Code
Expected behavior:
Not type error
Actual behavior:
Type mismatch:
Property 'groups' does not exist on type 'RegExprMatchArray'
Playground Link:
reproduced on stackblitz using the default typescript project
The text was updated successfully, but these errors were encountered: