File tree 2 files changed +8
-8
lines changed 2 files changed +8
-8
lines changed Original file line number Diff line number Diff line change @@ -4021,20 +4021,20 @@ NoInfer 是一种实用类型,旨在阻止泛型函数范围内类型的自动
4021
4021
` ` ` typescript
4022
4022
// 泛型函数范围内类型的自动推断。
4023
4023
function fn<T extends string>(x: T[], y: T) {
4024
- return x.concat(y);
4024
+ return x.concat(y);
4025
4025
}
4026
- const r = fn(['a', 'b' ], 'c' ); // 此处的类型为 ("a" | "b" | "c")[]
4026
+ const r = fn(["a", "b" ], "c" ); // 此处的类型为 ("a" | "b" | "c")[]
4027
4027
` ` `
4028
4028
4029
4029
使用 NoInfer :
4030
4030
4031
4031
` ` ` typescript
4032
4032
// 使用 NoInfer 阻止类型推断的示例函数
4033
4033
function fn2<T extends string>(x: T[], y: NoInfer<T>) {
4034
- return x.concat(y);
4034
+ return x.concat(y);
4035
4035
}
4036
4036
4037
- const r2 = fn2(['a', 'b' ], 'c' ); // 错误:类型为“c”的类型参数不能分配给类型为“a”|“b”的参数。
4037
+ const r2 = fn2(["a", "b" ], "c" ); // 错误:类型为“c”的类型参数不能分配给类型为“a”|“b”的参数。
4038
4038
` ` `
4039
4039
4040
4040
## 其他
Original file line number Diff line number Diff line change @@ -4054,20 +4054,20 @@ Example:
4054
4054
``` typescript
4055
4055
// Automatic inference of types within the scope of a generic function.
4056
4056
function fn<T extends string >(x : T [], y : T ) {
4057
- return x .concat (y );
4057
+ return x .concat (y );
4058
4058
}
4059
- const r = fn ([' a ' , ' b ' ], ' c ' ); // Type here is ("a" | "b" | "c")[]
4059
+ const r = fn ([" a " , " b " ], " c " ); // Type here is ("a" | "b" | "c")[]
4060
4060
```
4061
4061
4062
4062
With NoInfer:
4063
4063
4064
4064
``` typescript
4065
4065
// Example function that uses NoInfer to prevent type inference
4066
4066
function fn2<T extends string >(x : T [], y : NoInfer <T >) {
4067
- return x .concat (y );
4067
+ return x .concat (y );
4068
4068
}
4069
4069
4070
- const r2 = fn2 ([' a ' , ' b ' ], ' c ' ); // Error: Type Argument of type '"c"' is not assignable to parameter of type '"a" | "b"'.
4070
+ const r2 = fn2 ([" a " , " b " ], " c " ); // Error: Type Argument of type '"c"' is not assignable to parameter of type '"a" | "b"'.
4071
4071
```
4072
4072
4073
4073
## Others
You can’t perform that action at this time.
0 commit comments