Skip to content

Commit 54f0d22

Browse files
committed
add NoInfer
1 parent baa9cde commit 54f0d22

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

README-zh_CN.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
- [Lowercase\<T\>](#lowercaset)
204204
- [Capitalize\<T\>](#capitalizet)
205205
- [Uncapitalize\<T\>](#uncapitalizet)
206+
- [NoInfer\<T\>](#noinfert)
206207
- [其他](#其他)
207208
- [错误和异常处理](#错误和异常处理)
208209
- [混合类](#混合类)
@@ -4010,6 +4011,32 @@ type MyType = Capitalize<'abc'>; // "Abc"
40104011
type MyType = Uncapitalize<'Abc'>; // "abc"
40114012
```
40124013

4014+
#### NoInfer\<T\>
4015+
4016+
NoInfer 是一种实用类型,旨在阻止泛型函数范围内类型的自动推断。
4017+
4018+
示例:
4019+
4020+
<!-- skip -->
4021+
```typescript
4022+
// 泛型函数范围内类型的自动推断。
4023+
function fn<T extends string>(x: T[], y: T) {
4024+
return x.concat(y);
4025+
}
4026+
const r = fn(['a', 'b'], 'c'); // 此处的类型为 ("a" | "b" | "c")[]
4027+
```
4028+
4029+
使用 NoInfer
4030+
4031+
```typescript
4032+
// 使用 NoInfer 阻止类型推断的示例函数
4033+
function fn2<T extends string>(x: T[], y: NoInfer<T>) {
4034+
return x.concat(y);
4035+
}
4036+
4037+
const r2 = fn2(['a', 'b'], 'c'); // 错误:类型为“c”的类型参数不能分配给类型为“a”|“b”的参数。
4038+
```
4039+
40134040
## 其他
40144041

40154042
### 错误和异常处理

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ An online version is available at:
202202
- [Lowercase\<T\>](#lowercaset)
203203
- [Capitalize\<T\>](#capitalizet)
204204
- [Uncapitalize\<T\>](#uncapitalizet)
205+
- [NoInfer\<T\>](#noinfert)
205206
- [Others](#others)
206207
- [Errors and Exception Handling](#errors-and-exception-handling)
207208
- [Mixin classes](#mixin-classes)
@@ -4043,6 +4044,32 @@ Uncapitalize the name of the input type T.
40434044
type MyType = Uncapitalize<'Abc'>; // "abc"
40444045
```
40454046

4047+
#### NoInfer\<T\>
4048+
4049+
NoInfer is a utility type designed to block the automatic inference of types within the scope of a generic function.
4050+
4051+
Example:
4052+
4053+
<!-- skip -->
4054+
```typescript
4055+
// Automatic inference of types within the scope of a generic function.
4056+
function fn<T extends string>(x: T[], y: T) {
4057+
return x.concat(y);
4058+
}
4059+
const r = fn(['a', 'b'], 'c'); // Type here is ("a" | "b" | "c")[]
4060+
```
4061+
4062+
With NoInfer:
4063+
4064+
```typescript
4065+
// Example function that uses NoInfer to prevent type inference
4066+
function fn2<T extends string>(x: T[], y: NoInfer<T>) {
4067+
return x.concat(y);
4068+
}
4069+
4070+
const r2 = fn2(['a', 'b'], 'c'); // Error: Type Argument of type '"c"' is not assignable to parameter of type '"a" | "b"'.
4071+
```
4072+
40464073
## Others
40474074

40484075
### Errors and Exception Handling

0 commit comments

Comments
 (0)