RegExp literal flag types #38671
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Search Terms
RegExp Regex literal flags as const narrowing
Suggestion
This is similar to the proposal for typing named capture groups #32098
It would be useful to narrow RegExp literals based on their flags as well. This could be modeled as type parameter(s) as in that proposal, or as a type intersection, which I will use here.
Use Cases
Regular expressions have different behavior when defined with different flags, and functions may have different behavior when they receive them, so they may want to distinguish the flags of a RegExp for overloads or allowed argument values.
Examples
In the standard library, String.prototype.match returns either the first match with its capture groups, or a list of all matches, depending on whether the argument is a global RegExp. This could be modeled as an overload:
I recently wrote some functions which only work with sticky RegExps, so I had to put checks at the top of each function:
I'd like to define the function instead as:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: