π Search Terms
regex capture group types inference
β
Viability Checklist
β Suggestion
I think it would be useful to have capture groups inside of regexps to be represented in the type of RegExpExecArray.groups
π Motivating Example
Given the following RegExp:
const groups = /^(?<initial>\w)\w*/g.exec("Bob").groups
The type of groups should be inferred as:
This may imply that the type of RegExp needs to be generic over the named capture groups, so something like this:
const initialRegexp: RegExp<'initial' | 'rest'> = /^(?<initial>\w)(?<rest>\w*)/g
π» Use Cases
- What do you want to use this for? Better type inference when using RegExps
- What shortcomings exist with current approaches? All keys of
groups are string which can lead to errors if accessing a property that isn't in the RegExp
- What workarounds are you using in the meantime? Casting to the expected type
π Search Terms
regex capture group types inference
β Viability Checklist
β Suggestion
I think it would be useful to have capture groups inside of regexps to be represented in the type of
RegExpExecArray.groupsπ Motivating Example
Given the following RegExp:
The type of
groupsshould be inferred as:This may imply that the type of
RegExpneeds to be generic over the named capture groups, so something like this:π» Use Cases
groupsarestringwhich can lead to errors if accessing a property that isn't in the RegExp