Skip to content

interface RegExpMatchArray in lib.es2018.regexp.d.ts #51292

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
WesleyMConner opened this issue Oct 24, 2022 · 5 comments
Closed

interface RegExpMatchArray in lib.es2018.regexp.d.ts #51292

WesleyMConner opened this issue Oct 24, 2022 · 5 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@WesleyMConner
Copy link

lib Update Request

Configuration Check

My compilation target is es2022 and my lib is the default.

Missing / Incorrect Definition

Currently in ts > lib > lib.es2018.regexp.d.ts:

interface RegExpMatchArray {
  groups?: {
    [key: string]: string
  }
}

Per the Sample Code below, it looks like the interface should be:

interface RegExpMatchArray {
  groups?: {
    [key: string]: string | undefined
                          ^^^^^^^^^^^
  }
}

Sample Code

Sample code

const s = 'The quick brown fox'
const m = /(?<a>The).*(?<b>quick).*(?<c>red)?.*(?<d>fox)/
const x = s.match(m)           // x: RegExpMatchArray | null
if (x) {
  const y = x.groups           // const y: { [key: string]: string; } | undefined
  if (y) {
    const a = y['a']           // string | undefined
    const b = y['b']           // string | undefined
    const c = y['c']           // string | undefined
    const d = y['d']           // string | undefined  
    console.log(y)
    //  => [Object: null prototype] {
    //       a: 'The',
    //       b: 'quick',
    //       c: undefined,
    //       d: 'fox'
    //     }
  }
}  

Documentation Link

https://github.com/microsoft/TypeScript/blob/main/lib/lib.es2018.regexp.d.ts

@MartinJohns
Copy link
Contributor

You should enable noUncheckedIndexedAccess. It's the intended solution.

@WesleyMConner
Copy link
Author

I already have "noUncheckedIndexedAccess": true," in tsconfig.json.

Let's say I want to write a function that consumes named CGs produced by string.match(RegExp)

Crude Placeholder Function

const operatesOnGroups = (x: {[key: string]: string}) => console.log(x)

There is no issue passing the data produced from a match into this placeholder function.

const s = 'The quick brown fox'
const m = /(?<a>The).*(?<b>quick).*(?<c>red)?.*(?<d>fox)/
const x = s.match(m)           // x: RegExpMatchArray | null
if (x) {
  const y = x.groups           // const y: { [key: string]: string; } | undefined
  if (y) {
    operatesOnGroups(y)
      => [Object: null prototype] {
           a: 'The',
           b: 'quick',
           c: undefined,
           d: 'fox'
         }
  }
} 

There is a problem if I want to simulate match output data for testing my function.

const testData = {
  a: 'The',
  b: 'quick',
  c: undefined,  // Simulating the undefined present in the match result.
  d: 'fox'
}
operatesOnGroups(testData)
  => error TS2345: Argument of type '{ a: string; b: string; c: undefined; d: string; }'
     is not assignable to parameter of type '{ [key: string]: string; }'.
     Property 'c' is incompatible with index signature.
       Type 'undefined' is not assignable to type 'string'.

For test data (with an embedded undefined) to work, I have to alter the signature of my placeholder function away from the signature present in ts > lib > lib.es2018.regexp.d.ts.

FROM:
const operatesOnGroups = (x: {[key: string]: string}) => console.log(x)
TO:
const operatesOnGroups = (x: {[key: string]: string | undefined}) => console.log(x)

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Oct 24, 2022
@RyanCavanaugh
Copy link
Member

This is the intended behavior of index signatures across the board.

@WesleyMConner
Copy link
Author

WesleyMConner commented Oct 24, 2022 via email

@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants