microsoft / microsoft/TypeScript

RegExp literal flag types

Ouverte
#38,671 3 commentaires 20 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Awaiting More Feedback Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

L’issue mentionne les littéraux RegExp et les overloads de la bibliothèque standard pour String.prototype.match, mais aucun fichier ni test. Commencez par localiser la gestion des littéraux RegExp par le compilateur et les définitions de la bibliothèque pour match. La finalisation doit inclure une décision de conception du typage, un narrowing à la compilation pour des flags tels que sticky et global, ainsi qu’une couverture des exemples présentés.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
javascript, typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.