File tree 2 files changed +54
-0
lines changed 2 files changed +54
-0
lines changed Original file line number Diff line number Diff line change 203
203
- [ Lowercase\< T\> ] ( #lowercaset )
204
204
- [ Capitalize\< T\> ] ( #capitalizet )
205
205
- [ Uncapitalize\< T\> ] ( #uncapitalizet )
206
+ - [ NoInfer\< T\> ] ( #noinfert )
206
207
- [ 其他] ( #其他 )
207
208
- [ 错误和异常处理] ( #错误和异常处理 )
208
209
- [ 混合类] ( #混合类 )
@@ -4010,6 +4011,32 @@ type MyType = Capitalize<'abc'>; // "Abc"
4010
4011
type MyType = Uncapitalize<'Abc'>; // "abc"
4011
4012
` ` `
4012
4013
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
+
4013
4040
## 其他
4014
4041
4015
4042
### 错误和异常处理
Original file line number Diff line number Diff line change @@ -202,6 +202,7 @@ An online version is available at:
202
202
- [ Lowercase\< T\> ] ( #lowercaset )
203
203
- [ Capitalize\< T\> ] ( #capitalizet )
204
204
- [ Uncapitalize\< T\> ] ( #uncapitalizet )
205
+ - [ NoInfer\< T\> ] ( #noinfert )
205
206
- [ Others] ( #others )
206
207
- [ Errors and Exception Handling] ( #errors-and-exception-handling )
207
208
- [ Mixin classes] ( #mixin-classes )
@@ -4043,6 +4044,32 @@ Uncapitalize the name of the input type T.
4043
4044
type MyType = Uncapitalize <' Abc' >; // "abc"
4044
4045
```
4045
4046
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
+
4046
4073
## Others
4047
4074
4048
4075
### Errors and Exception Handling
You can’t perform that action at this time.
0 commit comments