打字稿箭头功能参数类型安全

时间:2016-05-02 04:15:45

标签: typescript angular

我的代码在" noImplicitAny":false时工作正常。

import ...;

@Injectable()
export class HeroService {
  private _cachedHeroes: Observable<Hero[]>; 
  private _init: boolean;
  private _heroesObserver: Observer<Hero[]>;
  private _heroObserver: Observer<Hero>;
  heroes$: Observable<Hero[]>; 
  hero$:   Observable<Hero>; 
  public _dataStore: { heroes: Hero[], hero: Hero };

  constructor (private http: Http) {
        this._init = true;
        this._dataStore = { heroes: [], hero: {_id: null, name: null} };
        this.heroes$ = new Observable((observer: any) =>  this._heroesObserver = observer).share();//.publishReplay(1).refCount();
        this.hero$   = new Observable((observer: any) =>  this._heroObserver = observer).share();
        this._baseUrl = 'http://localhost:8081/api/hero/';  
  }

  loadHero1(id: number) {
      this.hero$ = this._cachedHeroes.map(heroes => heroes.find(hero => {hero._id === id;})) 
                                     .catch(handleError)
                                     .subscribe( data => {  
                                                            this._dataStore.hero = data;
                                                            this._heroObserver.next(this._dataStore.hero);
                                                         },  
                                                  error => handleError('Could not load hero.')
                                               );
  }
  .......
}        

由于我想使其类型安全,我将tsconfig.json更改为 &#34; noImplicitAny&#34;:true。

然后我收到以下错误

[0] services/hero.service.ts(58,7): error TS2322: Type 'Subscription' is not assignable to type 'Observable<Hero>'.
[0]   Property '_isScalar' is missing in type 'Subscription'.
[1] [BS] File changed: dist\services\hero.js
[1] [BS] File changed: dist\services\common.js
[0] services/hero.service.ts(58,65): error TS2345: Argument of type '(hero: Hero) => void' is not assignable to parameter of type '(value: Hero, index: number, obj: Hero[]) => boolean'.
[0]   Type 'void' is not assignable to type 'boolean'.

以下是我的问题

  1. 如何从订阅类型转换this._cachedHeroes.map()。subscribe()到类型Observable以解决TS2322错误?我尝试了<Observable<Hero[]>>.this._cachedHeroes....,但没有用。
  2. 如何定义TypeScript箭头函数的参数类型以解决TS2345错误?我尝试了heroes.find( (hero: Hero) => {hero._id === id;}),但它没有用。
  3. 如何在以下代码中将显式any更改为Observer类型?

    this.hero $ = new Observable((observer:any)=&gt; this._heroObserver = observer).share();

  4. 任何建议表示赞赏。

2 个答案:

答案 0 :(得分:2)

在@GünterZöchbauer的帮助下,我把这件事理顺了。因此添加return语句将解决类型不匹配问题。以下是通过编译器检查的修改后的代码。

  loadHero1(id: number) {
      this.hero$ = this._cachedHeroes.map(heroes => heroes.find(hero => { return hero._id === id; } )) 
                                     .catch(handleError)
                                     .map( (data : Hero) => {  
                                                            this._dataStore.hero = data;
                                                            this._heroObserver.next(this._dataStore.hero);
                                                            return data;
                                                         } 
                                                  //error => handleError('Could not load hero.')
                                               );
  }

答案 1 :(得分:1)

subscribe()返回Subscription,而不是Observable

如果您将subscribe()更改为map(),则应该

  loadHero1(id: number) {
    this.hero$ = this._cachedHeroes
    .map(heroes => heroes.find(hero => {hero._id === id;})) 
    .catch(handleError)
    .map( data => {  
      this._dataStore.hero = data;
      this._heroObserver.next(this._dataStore.hero);
    });
  }

或者改变

heroes$: Observable<Hero[]>; 

heroes$: Subscription;

但我认为这不是您的代码的意图。