Iva*_*van 12 type-inference rxjs typescript
2月28日更新:关闭该问题,转而支持新问题.
在下面的代码片段中,如果查看最后两行,TypeScript会在第一行中显示错误,并在第二行中正确推断类型,尽管差异只是函数通过管道传递的顺序.
const pipe = <A, B, C>(
x: A,
a: (x: A) => B,
b: (x: B) => C,
) => b(a(x));
// This just calls the function passed as argument.
const call = <A, B>(f: (x: A) => B) => (x: A) => f(x)
const a = pipe(1, x => x + 1, call(x => x + 1));
const b = pipe(1, call(x => x + 1), x => x + 1);
Run Code Online (Sandbox Code Playgroud)
我在严格模式下使用TypeScript 2.7.1(包括strictFunctionTypes),但严格模式似乎并不重要.这是TypeScript操场上的这个片段.
这是我在使用RxJS时经常遇到的一个问题,因为在RxJS中有一个类似的pipe方法,我在使用创建运算符(如obs => merge(obs, otherObs))时将箭头函数传递给它.通常通过指定参数类型很容易解决这个问题,但我想了解这背后的逻辑.为什么TypeScript能够在一种情况下推断出类型但在另一种情况下却不能推断?
这个问题仍然悬而未决,但现在我想发布一个我想出的解决方法,因为我发现它在我的经验中非常有用。
创建一个像这样的实用函数:
export const call = <A, B>(f: (x: A) => B) => f;
Run Code Online (Sandbox Code Playgroud)
(与问题中的函数相同,一个以一元函数作为参数的恒等函数),然后每当您在管道中遇到箭头函数的问题时,请尝试用此实用程序函数将其括起来。与直觉相反,TS 无法处理这个问题:
const a = pipe(1, x => x + 1, call(x => x + 1));
Run Code Online (Sandbox Code Playgroud)
但它可以处理这个:
const c = pipe(1, call(x => x + 1), call(x => x + 1));
Run Code Online (Sandbox Code Playgroud)
(不知道为什么)。稍后,当问题得到解决时(继续投票: https: //github.com/Microsoft/TypeScript/issues/22081),您将能够轻松找到所有引用call并将其删除。
| 归档时间: |
|
| 查看次数: |
543 次 |
| 最近记录: |