microsoft / microsoft/TypeScript
RegExp literal flag types
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
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.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
この issue では RegExp リテラルと標準ライブラリの String.prototype.match overloads が挙げられていますが、ファイルやテストは示されていません。まず、RegExp リテラルを処理する compiler の箇所と、match の library definitions を特定してください。完了条件には、typing design の決定、sticky や global などの flags に対する compile-time narrowing、そして示されている例をカバーすることを含めてください。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- javascript, typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100