microsoft / microsoft/TypeScript

Provide a ReadonlyRegExp type

未关闭
#37,751 2 条评论 4 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Awaiting More Feedback Suggestion
主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 19 小时
30 天内合并 PR
117

描述

Search Terms

readonlyregexp readonly regexp

Suggestion

Provide a new ReadonlyRegExp type that represents a read only RegExp.

Use Cases

This is similar to other readonly types, e.g. ReadonlyArray -- I prefer my code to be immutable by default. I have a DeepReadonly helper that could map RegExp to ReadonlyRegExp.

Note that Readonly<RegExp> does not work:

'a'.replace(/a/ as Readonly<RegExp>, 'b')

/*
No overload matches this call.
  The last overload gave the following error.
    Argument of type 'Readonly<RegExp>' is not assignable to parameter of type 'string | RegExp'.
      Type 'Readonly<RegExp>' is missing the following properties from type 'RegExp': [Symbol.match], [Symbol.replace], [Symbol.search], [Symbol.split](2769)
*/

Examples

const emailRegExp = /[a-z]@[a-z\.]/ as ReadonlyRegExp;

export function suppressPII(message: string) {
    return message.replace(emailRegExp, '');
}

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.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

该 issue 未指出任何实现文件、测试或入口点。首先追踪 TypeScript 如何声明 RegExp 和现有的 readonly 类型,然后确定所示 replace 示例所需的声明和兼容性更改。完成标准是:ReadonlyRegExp 类型可用,并且可以在示例需要 RegExp 的位置使用。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, typescript
领域
compilers
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
描述清楚
新手友好度
30/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。