@@ -186,31 +186,31 @@ JavaScript 开发者不能简单地导入一个名为 `SomeType` 的类型,如
186
186
``` ts
187
187
// ./some-module.d.ts
188
188
export interface SomeType {
189
- // ...
189
+ // ...
190
190
}
191
191
192
192
// ./index.js
193
- import { SomeType } from " ./some-module" ; // runtime error!
193
+ import { SomeType } from ' ./some-module' ; // runtime error!
194
194
195
195
/**
196
196
* @param {SomeType} myValue
197
197
*/
198
198
function doSomething(myValue ) {
199
- // ...
199
+ // ...
200
200
}
201
201
```
202
202
203
203
` SomeType ` 类型在运行时不存在,因此导入会失败。
204
204
开发者可以使用命名空间导入来替代。
205
205
206
206
``` ts
207
- import * as someModule from " ./some-module" ;
207
+ import * as someModule from ' ./some-module' ;
208
208
209
209
/**
210
210
* @param {someModule.SomeType} myValue
211
211
*/
212
212
function doSomething(myValue ) {
213
- // ...
213
+ // ...
214
214
}
215
215
```
216
216
@@ -223,7 +223,7 @@ function doSomething(myValue) {
223
223
* @param {import("./some-module").SomeType} myValue
224
224
*/
225
225
function doSomething(myValue ) {
226
- // ...
226
+ // ...
227
227
}
228
228
```
229
229
@@ -238,7 +238,7 @@ function doSomething(myValue) {
238
238
* @param {SomeType} myValue
239
239
*/
240
240
function doSomething(myValue ) {
241
- // ...
241
+ // ...
242
242
}
243
243
```
244
244
@@ -253,7 +253,7 @@ function doSomething(myValue) {
253
253
* @param {SomeType} myValue
254
254
*/
255
255
function doSomething(myValue ) {
256
- // ...
256
+ // ...
257
257
}
258
258
```
259
259
@@ -267,11 +267,67 @@ function doSomething(myValue) {
267
267
* @param {someModule.SomeType} myValue
268
268
*/
269
269
function doSomething(myValue ) {
270
- // ...
270
+ // ...
271
271
}
272
272
```
273
273
274
274
因为它们是 JSDoc 注释,它们完全不影响运行时行为。
275
275
276
276
更多详情请参考[ PR] ( https://github.com/microsoft/TypeScript/pull/57207 ) 。
277
277
感谢 [ 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