microsoft / microsoft/TypeScript
RegExp literal flag types
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
Search Terms
RegExp Regex literal flags as const narrowing
Suggestion
This is similar to the proposal for typing named capture groups https://github.com/microsoft/TypeScript/issues/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.
const myRegExp = /hello/y; // has type RegExp & {dotAll: false, global: false, ignoreCase: false, multiline: false, sticky: true, unicode: false, flags: 'y'};
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:
match(str: string): RegExpMatchArray|null
match(regexp: RegExp & {global: false}): RegExpMatchArray|null
match(regexp: RegExp & {global: true}): Array<string>|null;
match(regexp: RegExp): RegExpMatchArray|null;
I recently wrote some functions which only work with sticky RegExps, so I had to put checks at the top of each function:
function match(regexp: RegExp) {
if (!regexp.sticky) { throw new Error("Invalid argument -- RegExp must use the /y 'sticky' flag"); }
...
}
match(/sticky example/y); // compiles and runs fine
match(/non-sticky example/); // compiles fine, runtime error
I'd like to define the function instead as:
type StickyRegExp = RegExp & {sticky: true};
function match(regexp: StickyRegExp) {
...
}
match(/sticky example/y); // compiles and runs fine
match(/non-sticky example/); // compiler error
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Das Issue nennt RegExp-Literale und die Overloads der Standardbibliothek für String.prototype.match, aber keine Dateien oder Tests. Beginne damit, die Compiler-Behandlung für RegExp-Literale und die Bibliotheksdefinitionen für match zu finden. Der Abschluss sollte ein festgelegtes Typing-Design, eine Compile-Time-Eingrenzung für Flags wie sticky und global sowie eine Abdeckung der gezeigten Beispiele umfassen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100