Typescript: Property 'groups' does not exist on type 'RegExprMatchArray' for named captured groups

Created on 17 Jan 2019  路  5Comments  路  Source: microsoft/TypeScript


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

External

Most helpful comment

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.

All 5 comments

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.

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

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

I still got this error in the latest 3.7.3, see more on https://github.com/microsoft/TypeScript/issues/35604.

Happens in the Typescript sandbox (target set to ES2020, Module ESNext).
https://www.typescriptlang.org/play/?target=7#code/MYewdgziA2CmB00QHMAUAiAViAFmA+gCYizrwC2AhgC7A6oD0AeqgPwA8AZgJYBOE1AHwBtSgFoAXgF0A1AEp4MgCQMFyXiACuABwjwe-anIAEDBsex4AUFdCQYCJGlTtKYAJ6CsuAsVIUaOkYWDgMBEXFpeUUVOTUNHT0wo1NzSzArIA

Was this page helpful?
0 / 5 - 0 ratings

Related issues

manekinekko picture manekinekko  路  3Comments

CyrusNajmabadi picture CyrusNajmabadi  路  3Comments

DanielRosenwasser picture DanielRosenwasser  路  3Comments

dlaberge picture dlaberge  路  3Comments

seanzer picture seanzer  路  3Comments