Skip to content

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

Closed
mateja176 opened this issue Jan 17, 2019 · 7 comments
Labels
External Relates to another program, environment, or user action which we cannot control.

Comments

@mateja176
Copy link

mateja176 commented Jan 17, 2019

TypeScript Version: 3.3.0-dev.201xxxxx

Search Terms:

Code

// Property 'groups' does not exist on type 'RegExprMatchArray
console.log("john_doe".match(/^(?<first>[a-z]+).+$/).groups.first) // john

console.log((<any>"john_doe".match(/^(?<first>[a-z]+).+$/)).groups.first) // john

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

@j-oliveras
Copy link
Contributor

j-oliveras commented Jan 18, 2019

You should set target to es2018 or higher (for example esnext) or add es2018.regexp into lib config.

Example 1 (target to es2018 or higher): tsconfig.json and using your sample code as is

{
  "compilerOptions": {
    "target": "es2018"
  }
}

Example 2 (using lib config): tsconfig.json and using your sample code as is

{
  "compilerOptions": {
    "target": "es2015",
    "lib": [ "es2015", "es2018.regexp" ]
  }
}

Example 3 (adding reference lib on code):
tsconfig.json

{
  "compilerOptions": {
    "target": "es2015" // probably can be lower
  }
}

file.ts

/// <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.

@mateja176
Copy link
Author

You should set target to es2018 or higher (for example esnext) or add es2018.regexp into lib config.

Example 1 (target to es2018 or higher): tsconfig.json and using your sample code as is

{
  "compilerOptions": {
    "target": "es2018"
  }
}

Example 2 (using lib config): tsconfig.json and using your sample code as is

{
  "compilerOptions": {
    "target": "es2015",
    "lib": [ "es2015", "es2018.regexp" ]
  }
}

Example 3 (adding reference lib on code):
tsconfig.json

{
  "compilerOptions": {
    "target": "es2015" // probably can be lower
  }
}

file.ts

/// <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

@mateja176 mateja176 reopened this Jan 18, 2019
@weswigham
Copy link
Member

stackblitz is running an older version of TS that doesn't even understand the lib reference directive syntax - so prior to 3.0.

@weswigham weswigham added the External Relates to another program, environment, or user action which we cannot control. label Jan 18, 2019
@liudonghua123
Copy link

I still got this error in the latest 3.7.3, see more on #35604.

@fisiel
Copy link

fisiel commented Oct 18, 2022

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);

@MartinJohns
Copy link
Contributor

@miccies3 Works fine. Playground link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
External Relates to another program, environment, or user action which we cannot control.
Projects
None yet
Development

No branches or pull requests

7 participants