angular - RxJS startWith在Angular 4应用程序中生成TypeScript编译器错误

标签 angular typescript compiler-errors rxjs5

使用startWith时,RxJS 5出现TypeScript编译器错误。但是,如果我将此(初始)值移到scan运算符中,则所有内容都会编译并运行良好。

我不知道错误的根源。如下所示,来自我的Angular 4测试应用程序的示例代码。

编译错误:

rxjs-counter: master$ ng s                                                                                                      
** NG Live Development Server is running on http://localhost:4200 **                                                            
Hash: ef256d342c83fd7c92a6                                                                                                      
Time: 15133ms                                                                                                                   
chunk    {0} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 165 kB {4} [initial] [rendered]                           
chunk    {1} main.bundle.js, main.bundle.js.map (main) 5.48 kB {3} [initial] [rendered]                                         
chunk    {2} styles.bundle.js, styles.bundle.js.map (styles) 9.77 kB {4} [initial] [rendered]                                   
chunk    {3} vendor.bundle.js, vendor.bundle.js.map (vendor) 2.98 MB [initial] [rendered]                                       
chunk    {4} inline.bundle.js, inline.bundle.js.map (inline) 0 bytes [entry] [rendered]                                         

ERROR in /home/yadav/dev/javascript/rxjs-counter/src/app/app.component.ts (46,19): Argument of type '{ count: number; }' is not 
assignable to parameter of type 'IScheduler | ((v: CounterData) => { count: number; })'.                                        
  Object literal may only specify known properties, and 'count' does not exist in type 'IScheduler | ((v: CounterData) => { coun
t: number; })'.                                                                                                                 
webpack: Failed to compile. 

示例模板:(app.component.html)
<h1>
  {{title}}
</h1>
<div>{{count}}</div>
<div>
  <button (click)="onStart()">Start</button>
  <button (click)="onStop()">Stop</button>
  <button (click)="onReset()">Reset</button>
</div>

示例代码:(app.component.ts)
import { Component } from '@angular/core';
import { Observable, Subject } from 'rxjs/Rx';

interface CounterData {
  count: number;
}

interface CounterFunc {
  (arg: CounterData);
}

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'Reactive Counter';
  count = 0;

  // Observable (streams)
  start$: Subject<number>;
  stop$: Subject<number>;
  reset$: Subject<number>;

  counter$: Observable<CounterData>;
  intervalUntil$: Observable<number>;

  constructor() {
    this.start$ = new Subject();
    this.stop$ = new Subject();
    this.reset$ = new Subject();

    // Observable that cancels on a stream.
    this.intervalUntil$ = Observable
      .interval(500)
      .takeUntil(this.stop$);

    // Notice that takeUntil is on the inner Observable, putting it on the outer Observable
    // would kill the entire Observable chain, and we would need to re-subscribe on it again.
    this.counter$ = this.start$
      .switchMapTo(Observable.merge(
          this.intervalUntil$.mapTo(this.incCount),
          this.reset$.mapTo(this.resetCount)
      ))
      .startWith({count: 0})
      .scan((acc: CounterData, curr: CounterFunc) => curr(acc));

    // Assign observer to cancelable interval stream.
    this.counter$
      .subscribe((v: CounterData) => {
        this.count = v.count;
        console.log(v);
      });
  }

  resetCount(v: CounterData) {
    return {count: 0};
  }

  incCount(v: CounterData) {
    return {count: v.count + 1};
  }

  onStart() {
    this.start$.next();
  }

  onStop() {
    this.stop$.next();
  }

  onReset() {
    this.reset$.next();
  }

}

最佳答案

您没有使用种子值初始化scan()http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-scan

您需要这样设置:

.scan((acc: CounterData, curr: CounterFunc) => curr(acc), {count: 0});

另外,现在您根本不需要使用startWith

关于angular - RxJS startWith在Angular 4应用程序中生成TypeScript编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43625294/

相关文章:

javascript - 误报 "Unreachable code detected.ts(7027)"?

c++ - 包含编译错误类 'does not name a type'争议头文件

java - 尝试编译代码 - 无法找到或加载主类 a

c++ - 输入std::array [duplicate]时遇到麻烦

angular - 如何从测试代码中触发 Angular Material MatSelect 上的 selectionChange 事件

javascript - Angular:服务与组件中的功能?

html - 如何从 Angular 中的 JSON 列表中获取值?

javascript - 无法分配给对象 'property' 的只读属性 '#<Object>'

依赖于另一个表单控件的 Angular 2 自定义验证器

angular - 将版权 header 添加到已编译的 Angular 应用程序