Skip to content

Commit e9ec1f7

Browse files
committed
正则表达式语法检查
5.5
1 parent cc1eff7 commit e9ec1f7

File tree

1 file changed

+65
-9
lines changed

1 file changed

+65
-9
lines changed

zh/release-notes/typescript-5.5.md

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,31 +186,31 @@ JavaScript 开发者不能简单地导入一个名为 `SomeType` 的类型,如
186186
```ts
187187
// ./some-module.d.ts
188188
export interface SomeType {
189-
// ...
189+
// ...
190190
}
191191

192192
// ./index.js
193-
import { SomeType } from "./some-module"; // runtime error!
193+
import { SomeType } from './some-module'; // runtime error!
194194

195195
/**
196196
* @param {SomeType} myValue
197197
*/
198198
function doSomething(myValue) {
199-
// ...
199+
// ...
200200
}
201201
```
202202

203203
`SomeType` 类型在运行时不存在,因此导入会失败。
204204
开发者可以使用命名空间导入来替代。
205205

206206
```ts
207-
import * as someModule from "./some-module";
207+
import * as someModule from './some-module';
208208

209209
/**
210210
* @param {someModule.SomeType} myValue
211211
*/
212212
function doSomething(myValue) {
213-
// ...
213+
// ...
214214
}
215215
```
216216

@@ -223,7 +223,7 @@ function doSomething(myValue) {
223223
* @param {import("./some-module").SomeType} myValue
224224
*/
225225
function doSomething(myValue) {
226-
// ...
226+
// ...
227227
}
228228
```
229229

@@ -238,7 +238,7 @@ function doSomething(myValue) {
238238
* @param {SomeType} myValue
239239
*/
240240
function doSomething(myValue) {
241-
// ...
241+
// ...
242242
}
243243
```
244244

@@ -253,7 +253,7 @@ function doSomething(myValue) {
253253
* @param {SomeType} myValue
254254
*/
255255
function doSomething(myValue) {
256-
// ...
256+
// ...
257257
}
258258
```
259259

@@ -267,11 +267,67 @@ function doSomething(myValue) {
267267
* @param {someModule.SomeType} myValue
268268
*/
269269
function doSomething(myValue) {
270-
// ...
270+
// ...
271271
}
272272
```
273273

274274
因为它们是 JSDoc 注释,它们完全不影响运行时行为。
275275

276276
更多详情请参考[PR](https://github.com/microsoft/TypeScript/pull/57207)
277277
感谢 [Oleksandr Tarasiuk](https://github.com/a-tarasyuk) 的贡献。
278+
279+
## 正则表达式语法检查
280+
281+
直到现在,TypeScript 通常会跳过代码中的大多数正则表达式。
282+
这是因为正则表达式在技术上具有可扩展的语法,TypeScript 从未努力将正则表达式编译成早期版本的 JavaScript。
283+
尽管如此,这意味着许多常见问题可能会在正则表达式中被忽略,并且它们要么会在运行时转变为错误,要么会悄悄地失败。
284+
285+
但是现在,TypeScript 对正则表达式进行基本的语法检查了!
286+
287+
```ts
288+
let myRegex = /@robot(\s+(please|immediately)))? do some task/;
289+
// ~
290+
// error!
291+
// Unexpected ')'. Did you mean to escape it with backslash?
292+
```
293+
294+
这是一个简单的例子,但这种检查可以捕捉到许多常见的错误。
295+
事实上,TypeScript 的检查略微超出了语法检查。
296+
例如,TypeScript 现在可以捕捉到不存在的后向引用周围的问题。
297+
298+
```ts
299+
let myRegex = /@typedef \{import\((.+)\)\.([a-zA-Z_]+)\} \3/u;
300+
// ~
301+
// error!
302+
// This backreference refers to a group that does not exist.
303+
// There are only 2 capturing groups in this regular expression.
304+
```
305+
306+
这同样适用于捕获命名的分组。
307+
308+
```ts
309+
let myRegex =
310+
/@typedef \{import\((?<importPath>.+)\)\.(?<importedEntity>[a-zA-Z_]+)\} \k<namedImport>/;
311+
// ~~~~~~~~~~~
312+
// error!
313+
// There is no capturing group named 'namedImport' in this regular expression.
314+
```
315+
316+
TypeScript 现在还会检测到当使用某些 RegExp 功能时,这些功能是否比您的 ECMAScript 目标版本更新。
317+
例如,如果我们在 ES5 目标中使用类似上文中的命名捕获组,将会导致错误。
318+
319+
```ts
320+
let myRegex =
321+
/@typedef \{import\((?<importPath>.+)\)\.(?<importedEntity>[a-zA-Z_]+)\} \k<importedEntity>/;
322+
// ~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
323+
// error!
324+
// Named capturing groups are only available when targeting 'ES2018' or later.
325+
```
326+
327+
同样适用于某些正则表达式标志。
328+
329+
请注意,TypeScript 的正则表达式支持仅限于正则表达式字面量。
330+
如果您尝试使用字符串字面量调用 `new RegExp`,TypeScript 将不会检查提供的字符串。
331+
332+
更多详情请参考[PR](https://github.com/microsoft/TypeScript/pull/55600)
333+
感谢 [graphemecluster](https://github.com/graphemecluster/) 的贡献。

0 commit comments

Comments
 (0)