microsoft / microsoft/TypeScript

RegExp literal flag types

Đang mở
#38,671 3 bình luận 20 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Awaiting More Feedback Suggestion
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Issue này nêu các RegExp literal và các overload của thư viện chuẩn cho String.prototype.match, nhưng không nêu file hay test nào. Hãy bắt đầu bằng việc xác định phần xử lý RegExp literal của compiler và các định nghĩa thư viện cho match. Việc hoàn thành cần bao gồm một quyết định về thiết kế typing, compile-time narrowing cho các flag như sticky và global, cùng coverage cho các ví dụ được nêu.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript, typescript
Lĩnh vực
compilers
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.