Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
gibbok committed Jun 2, 2024
1 parent d4b56e1 commit dfd5678
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4021,20 +4021,20 @@ NoInfer 是一种实用类型,旨在阻止泛型函数范围内类型的自动
```typescript
// 泛型函数范围内类型的自动推断。
function fn<T extends string>(x: T[], y: T) {
return x.concat(y);
return x.concat(y);
}
const r = fn(['a', 'b'], 'c'); // 此处的类型为 ("a" | "b" | "c")[]
const r = fn(["a", "b"], "c"); // 此处的类型为 ("a" | "b" | "c")[]
```

使用 NoInfer

```typescript
// 使用 NoInfer 阻止类型推断的示例函数
function fn2<T extends string>(x: T[], y: NoInfer<T>) {
return x.concat(y);
return x.concat(y);
}
const r2 = fn2(['a', 'b'], 'c'); // 错误:类型为“c”的类型参数不能分配给类型为“a”|“b”的参数。
const r2 = fn2(["a", "b"], "c"); // 错误:类型为“c”的类型参数不能分配给类型为“a”|“b”的参数。
```

## 其他
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4054,20 +4054,20 @@ 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);
return x.concat(y);
}
const r = fn(['a', 'b'], 'c'); // Type here is ("a" | "b" | "c")[]
const r = fn(["a", "b"], "c"); // Type here is ("a" | "b" | "c")[]
```

With NoInfer:

```typescript
// Example function that uses NoInfer to prevent type inference
function fn2<T extends string>(x: T[], y: NoInfer<T>) {
return x.concat(y);
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"'.
const r2 = fn2(["a", "b"], "c"); // Error: Type Argument of type '"c"' is not assignable to parameter of type '"a" | "b"'.
```

## Others
Expand Down

0 comments on commit dfd5678

Please sign in to comment.