-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Open
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
Search Terms
named regexp, named capture groups
Motivation
Currently named capture groups are a bit of a pain in TypeScript:
- All names are of type string even if the regexp doesn't have that named group.
- You need to use non-null assertion for
.groups
even when that is the only possibility.
Suggestion
I propose making RegExp
higher order on its named capture groups so that .groups
is well typed.
// Would have type: RegExp<{ year: string, month: string }>
const date = /(?<year>[0-9]{4})-(?<month>[0-9]{2})/
const match = someString.match(date)
if (match) {
// match.groups type would be { year: string, month: string }
// currently is undefined | { [key: string]: string }
}
// Would have type RegExp<{ year: string, month?: string }>
const optionalMonth = /(?<year>[0-9]{4})(-(?<month>[0-9]{2}))?/
Checklist
My suggestion meets these guidelines:
- [✓] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [✓] This wouldn't change the runtime behavior of existing JavaScript code
- [✓] This could be implemented without emitting different JS based on the types of the expressions
- [✓] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- [✓] This feature would agree with the rest of TypeScript's Design Goals.
IllusionMH, felixfbecker, fider, Jessidhia, kbradl16 and 89 more
Metadata
Metadata
Assignees
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript