TS2322:输入'Observable&lt; {}&gt;'不能分配给'Observable <hero>'

时间:2016-05-07 16:09:29

标签: typescript angular

这里是hero.service.ts构造函数

@Injectable()
export class HeroService {
  private _heroObserver: Observer<Hero>;
  hero$:   Observable<Hero>; 
  public errorMessage: string;

  constructor (private http: Http) {
    this.hero$   = new Observable((observer:any) =>  this._heroObserver = observer).share();  // line 29
  }

它编译错误

[0] hero.service.ts(29,9): error TS2322: Type 'Observable<{}>' is
not assignable to type 'Observable<Hero>'.
[0]   Type '{}' is not assignable to type 'Hero'.
[0]     Property '_id' is missing in type '{}'.

提到的类型“{}”是什么以及如何修复它?

PS:这是在beta-16上。

1 个答案:

答案 0 :(得分:2)

我认为这应该有效:

this.hero$   = new Observable<Hero>((observer:any) =>  this._heroObserver = observer).share();  // line 29