angular - 如何使用扫描运算符来计算 void observable 的发射值?
问题描述
我需要一个 void 类型的可观察对象,它发出发出的 void 值的数量。
const subject = new Subject<void>();
subject.pipe(
scan((acc, curr) => acc + 1, 0)
).subscribe(count => console.log(count));
subject.next(); // should output 1
subject.next(); // should output 2
subject.next(); // should output 3
上面给出了以下编译器错误:
TS2345: Argument of type 'MonoTypeOperatorFunction<number>' is not
assignable to parameter of type 'OperatorFunction<void, number>'.
Types of parameters 'source' and 'source' are incompatible.
Type 'Observable<void>' is not assignable to type 'Observable<number>'.
Type 'void' is not assignable to type 'number'.
也许我只是累了,但我似乎无法修复错误。我看不出我的scan()
接线员有什么问题。
解决方案
为了解决您的问题,您可以为传递给的函数的参数指定类型scan
,如下所示:
subject.pipe(
scan((acc: number, curr: void) => acc + 1, 0)
).subscribe(count => console.log(count));
需要注意的scan
类型。reduce
基本上,它们就是这样,因为它们需要像旧版本的 TypeScript 那样。现在 TypeScript 2.8 是 RxJS 6 的最低支持版本,应该可以改进类型。
推荐阅读
- node.js - “top”命令中的nodejs max-old-space-size 和 VSZ
- eclipse - 我有问题,cmd无法编译build.gradle
- .net - oracle 19c 和 .net (mvc) 之间的兼容性
- phpspreadsheet - PHPSpreadsheet条形图 - 在模板中定义X轴时无法打开生成的电子表格
- python - 对文本内的所有数字进行操作
- react-transition-group - 如何淡出一个反应组件,然后淡入另一个?
- html - 将引导范围颜色更改为十六进制颜色?
- xamarin - 尝试从导航返回 bool 时从 5.x 更新到 8.0.2 后关闭和导航编译错误
- elasticsearch - 使用 searchAfter(jump to a specific page,) 从大堆栈的中间获取数据
- angular - 升级到 Angular 12