Skip to content

Commit 0452600

Browse files
committed
update book with content 5.3 and 5.4
1 parent d5c3e6c commit 0452600

File tree

6 files changed

+92
-0
lines changed

6 files changed

+92
-0
lines changed

website/src/content/docs/book/others.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,3 +932,21 @@ Connection closed.
932932
```
933933

934934
The `using` and `await using` declarations are allowed in Statements: `for`, `for-in`, `for-of`, `for-await-of`, `switch`.
935+
936+
### Import Attributes
937+
938+
TypeScript 5.3's Import Attributes (labels for imports) tell the runtime how to handle modules (JSON, etc.). This improves security by ensuring clear imports and aligns with Content Security Policy (CSP) for safer resource loading. TypeScript ensures they're valid but lets the runtime handle their interpretation for specific module handling.
939+
940+
Example:
941+
942+
<!-- skip -->
943+
```typescript
944+
import config from './config.json' with { type: 'json' };
945+
```
946+
947+
with dynamic import:
948+
949+
<!-- skip -->
950+
```typescript
951+
const config = import('./config.json', { with: { type: 'json' } });
952+
```

website/src/content/docs/book/table-of-contents.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ sidebar:
182182
- Lowercase\<T\>
183183
- Capitalize\<T\>
184184
- Uncapitalize\<T\>
185+
- NoInfer\<T\>
185186
- Others
186187
- Errors and Exception Handling
187188
- Mixin classes
@@ -215,5 +216,6 @@ sidebar:
215216
- Type-Only Imports and Export
216217
- using declaration and Explicit Resource Management
217218
- await using declaration
219+
- Import Attributes
218220
<!-- markdownlint-enable MD004 -->
219221

website/src/content/docs/book/type-manipulation.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,29 @@ Uncapitalize the name of the input type T.
333333
type MyType = Uncapitalize<'Abc'>; // "abc"
334334
```
335335

336+
#### NoInfer\<T\>
337+
338+
NoInfer is a utility type designed to block the automatic inference of types within the scope of a generic function.
339+
340+
Example:
341+
342+
```typescript
343+
// Automatic inference of types within the scope of a generic function.
344+
function fn<T extends string>(x: T[], y: T) {
345+
return x.concat(y);
346+
}
347+
const r = fn(['a', 'b'], 'c'); // Type here is ("a" | "b" | "c")[]
348+
```
349+
350+
With NoInfer:
351+
352+
<!-- skip -->
353+
```typescript
354+
// Example function that uses NoInfer to prevent type inference
355+
function fn2<T extends string>(x: T[], y: NoInfer<T>) {
356+
return x.concat(y);
357+
}
358+
359+
const r2 = fn2(['a', 'b'], 'c'); // Error: Type Argument of type '"c"' is not assignable to parameter of type '"a" | "b"'.
360+
```
361+

website/src/content/docs/zh-cn/book/others.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,3 +914,21 @@ Connection closed.
914914
```
915915

916916
语句中允许使用"using"和"await using"声明:"for"、"for-in"、"for-of"、"for-await-of"、"switch"。
917+
918+
### 导入属性
919+
920+
TypeScript 5.3 的导入属性(导入标签)告诉运行时如何处理模块(JSON 等)。这通过确保干净的导入来提高安全性,并与内容安全策略 (CSP) 保持一致,以实现更安全的资源加载。TypeScript 确保它们有效,但让运行时处理它们的解释以进行特定的模块处理。
921+
922+
示例:
923+
924+
<!-- skip -->
925+
```typescript
926+
import config from './config.json' with { type: 'json' };
927+
```
928+
929+
使用动态导入:
930+
931+
<!-- skip -->
932+
```typescript
933+
const config = import("./config.json", { with: { type: "json" } })
934+
```

website/src/content/docs/zh-cn/book/table-of-contents.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ sidebar:
183183
- Lowercase\<T\>
184184
- Capitalize\<T\>
185185
- Uncapitalize\<T\>
186+
- NoInfer\<T\>
186187
- 其他
187188
- 错误和异常处理
188189
- 混合类
@@ -215,5 +216,6 @@ sidebar:
215216
- 仅类型导入和导出
216217
- 使用声明和显式资源管理
217218
- 使用声明等待
219+
- 导入属性
218220
<!-- markdownlint-enable MD004 -->
219221

website/src/content/docs/zh-cn/book/type-manipulation.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,29 @@ type MyType = Capitalize<'abc'>; // "Abc"
333333
type MyType = Uncapitalize<'Abc'>; // "abc"
334334
```
335335

336+
#### NoInfer\<T\>
337+
338+
NoInfer 是一种实用类型,旨在阻止泛型函数范围内类型的自动推断。
339+
340+
示例:
341+
342+
```typescript
343+
// 泛型函数范围内类型的自动推断。
344+
function fn<T extends string>(x: T[], y: T) {
345+
return x.concat(y);
346+
}
347+
const r = fn(['a', 'b'], 'c'); // 此处的类型为 ("a" | "b" | "c")[]
348+
```
349+
350+
使用 NoInfer:
351+
352+
<!-- skip -->
353+
```typescript
354+
// 使用 NoInfer 阻止类型推断的示例函数
355+
function fn2<T extends string>(x: T[], y: NoInfer<T>) {
356+
return x.concat(y);
357+
}
358+
359+
const r2 = fn2(["a", "b"], "c"); // 错误:类型为“c”的类型参数不能分配给类型为“a”|“b”的参数。
360+
```
361+

0 commit comments

Comments
 (0)