Typescript: RegExpExecArray is missing a type definition for the groups property

Created on 14 Apr 2019  路  8Comments  路  Source: microsoft/TypeScript


TypeScript Version: I'm using the ts checker with javascript in VS Code so I'm not 100% sure on the version I'm using.


Search Terms:
RegExpExecArray
RegExpExecArray groups
exec groups
regex groups

Code

// GF116 [GeForce GTX 550 Ti] --> GeForce GTX 550 Ti
const regex = new RegExp(/.+\[(?<gpuname>.+)\]/);
regex.exec('GF116 [GeForce GTX 550 Ti]').groups.gpuname;

Expected behavior:
No error.

Actual behavior:
Property 'groups' does not exist on type 'RegExpExecArray'.ts(2339)

Related Issues:
https://github.com/Microsoft/TypeScript/issues/17094

Question

Most helpful comment

@RyanCavanaugh please reopen this.

All 8 comments

Duplicate of #29465.

edit: .. or not? The other issue is about RegExprMatchArray. Not sure what the difference is.

@MartinJohns looks like RegExpExecArray also needs the fix.

Trying the stackblitz from that issue with this code still shows this.

Property 'groups' does not exist on type 'RegExpExecArray'.
// GF116 [GeForce GTX 550 Ti] --> GeForce GTX 550 Ti
const regex = new RegExp(/.+\[(?<gpuname>.+)\]/);
regex.exec('GF116 [GeForce GTX 550 Ti]').groups.gpuname;

Trying the stackblitz from that issue with this code still shows this.

The issue explicitly says that the Stackblitz website is using outdated versions...

When you set your target to es2018, then your code works. https://github.com/Microsoft/TypeScript/issues/29465#issuecomment-455489648

Trying this on the playground again no longer works. I tried es2018, es2019, es2020 and esnext.

Version: v3.9.0-dev.20200407
Config:
Screen Shot 2020-04-09 at 1 17 29 pm

const regex = new RegExp(/^.+: ID (?<id>\S+)(?<name>.*)$/);
const line = 'test string';
const groups = regex.exec(line)?.groups;

Hovering over ?.groups shows "Property 'groups' does not exist on type 'RegExpExecArray'.(2339)".

Same on 3.9.3 for me.

For now, i'm doing something like this:

  type AdjustedRegex = RegExpExecArray & { groups: {} };
  const matches = regex.exec(line) as AdjustedRegex;

This is still an issue. Can it please be reopened?

Still an issue, target & lib => 2020

@RyanCavanaugh please reopen this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dlaberge picture dlaberge  路  3Comments

weswigham picture weswigham  路  3Comments

uber5001 picture uber5001  路  3Comments

jbondc picture jbondc  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments