Skip to content

Commit

Permalink
update book with content 5.3 and 5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
gibbok committed Jun 2, 2024
1 parent d5c3e6c commit 0452600
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 0 deletions.
18 changes: 18 additions & 0 deletions website/src/content/docs/book/others.md
Original file line number Diff line number Diff line change
Expand Up @@ -932,3 +932,21 @@ Connection closed.
```

The `using` and `await using` declarations are allowed in Statements: `for`, `for-in`, `for-of`, `for-await-of`, `switch`.

### Import Attributes

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.

Example:

<!-- skip -->
```typescript
import config from './config.json' with { type: 'json' };
```

with dynamic import:

<!-- skip -->
```typescript
const config = import('./config.json', { with: { type: 'json' } });
```
2 changes: 2 additions & 0 deletions website/src/content/docs/book/table-of-contents.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ sidebar:
- Lowercase\<T\>
- Capitalize\<T\>
- Uncapitalize\<T\>
- NoInfer\<T\>
- Others
- Errors and Exception Handling
- Mixin classes
Expand Down Expand Up @@ -215,5 +216,6 @@ sidebar:
- Type-Only Imports and Export
- using declaration and Explicit Resource Management
- await using declaration
- Import Attributes
<!-- markdownlint-enable MD004 -->

26 changes: 26 additions & 0 deletions website/src/content/docs/book/type-manipulation.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,29 @@ Uncapitalize the name of the input type T.
type MyType = Uncapitalize<'Abc'>; // "abc"
```

#### NoInfer\<T\>

NoInfer is a utility type designed to block the automatic inference of types within the scope of a generic function.

Example:

```typescript
// Automatic inference of types within the scope of a generic function.
function fn<T extends string>(x: T[], y: T) {
return x.concat(y);
}
const r = fn(['a', 'b'], 'c'); // Type here is ("a" | "b" | "c")[]
```

With NoInfer:

<!-- skip -->
```typescript
// Example function that uses NoInfer to prevent type inference
function fn2<T extends string>(x: T[], y: NoInfer<T>) {
return x.concat(y);
}

const r2 = fn2(['a', 'b'], 'c'); // Error: Type Argument of type '"c"' is not assignable to parameter of type '"a" | "b"'.
```

18 changes: 18 additions & 0 deletions website/src/content/docs/zh-cn/book/others.md
Original file line number Diff line number Diff line change
Expand Up @@ -914,3 +914,21 @@ Connection closed.
```

语句中允许使用"using"和"await using"声明:"for"、"for-in"、"for-of"、"for-await-of"、"switch"。

### 导入属性

TypeScript 5.3 的导入属性(导入标签)告诉运行时如何处理模块(JSON 等)。这通过确保干净的导入来提高安全性,并与内容安全策略 (CSP) 保持一致,以实现更安全的资源加载。TypeScript 确保它们有效,但让运行时处理它们的解释以进行特定的模块处理。

示例:

<!-- skip -->
```typescript
import config from './config.json' with { type: 'json' };
```

使用动态导入:

<!-- skip -->
```typescript
const config = import("./config.json", { with: { type: "json" } })
```
2 changes: 2 additions & 0 deletions website/src/content/docs/zh-cn/book/table-of-contents.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ sidebar:
- Lowercase\<T\>
- Capitalize\<T\>
- Uncapitalize\<T\>
- NoInfer\<T\>
- 其他
- 错误和异常处理
- 混合类
Expand Down Expand Up @@ -215,5 +216,6 @@ sidebar:
- 仅类型导入和导出
- 使用声明和显式资源管理
- 使用声明等待
- 导入属性
<!-- markdownlint-enable MD004 -->

26 changes: 26 additions & 0 deletions website/src/content/docs/zh-cn/book/type-manipulation.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,29 @@ type MyType = Capitalize<'abc'>; // "Abc"
type MyType = Uncapitalize<'Abc'>; // "abc"
```

#### NoInfer\<T\>

NoInfer 是一种实用类型,旨在阻止泛型函数范围内类型的自动推断。

示例:

```typescript
// 泛型函数范围内类型的自动推断。
function fn<T extends string>(x: T[], y: T) {
return x.concat(y);
}
const r = fn(['a', 'b'], 'c'); // 此处的类型为 ("a" | "b" | "c")[]
```

使用 NoInfer:

<!-- skip -->
```typescript
// 使用 NoInfer 阻止类型推断的示例函数
function fn2<T extends string>(x: T[], y: NoInfer<T>) {
return x.concat(y);
}

const r2 = fn2(["a", "b"], "c"); // 错误:类型为“c”的类型参数不能分配给类型为“a”|“b”的参数。
```

0 comments on commit 0452600

Please sign in to comment.