2016-05-02 10 views
1

"noImplicitAny":falseのときにうまくいくコードがあります。typescript arrow関数のパラメータ型safe

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.') 
               ); 
    } 
    ....... 
}   

私はそれをタイプセーフにしたいので、私は 「noImplicitAny」にtsconfig.jsonを変更:真。

その後、私は解決するために観察可能な入力するタイプのサブスクリプションから)ここで

[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キャストすることができます)です。(購読、次のエラーを得ましたTS2322エラー?私は<Observable<Hero[]>>.this._cachedHeroes....を試しましたが、動作しませんでした。
  2. TS2345エラーを解決するためにTypeScript矢印関数の引数の型を定義するにはどうすればよいですか?私はheroes.find((hero: Hero) => {hero._id === id;})を試しましたが、うまくいきませんでした。
  3. 明示的なanyをObserverの型に変更するにはどうすればよいですか?

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

アドバイスをいただければ幸いです。

+0

「hero.service.ts(58,65)」に印を付けることができますか? – Dinistro

答えて

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

subscribe()あなたはsubscribe()map()に変更するとObservable

それは

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); 
    }); 
    } 

を仕事または代わりに変更する必要がありますSubscription、ない返し

heroes$: Observable<Hero[]>; 

heroes$: Subscription; 

しかし、私はあなたのコードの意図ではないと思います。

+0

迅速な対応に感謝します。 2番目の.map()はsubscript()メソッドの同じ目的を果たしますか?そして、私はちょうど3番目の質問を追加しました:)本当にあなたの答えを試みます。 – Shawn

+1

いいえ、目的は異なります。 'subscribe()'はobservableを 'map()'が実行しない間に実行します。 'subscribe()'によって返される 'Subscription'は' subscr.unsubscribe() 'でサブスクリプションを取り消すことができます。 (私の答えのコードのような)観測可能なデータからデータを受け取る場合は、最初に 'hero $ .subscribe()'や '

{{hero$ | async}}
'のようにsubscribeする必要があります。 –

+0

subscribe()をmapエラー:[0] services/hero.service.ts(58,7):エラーTS2322:タイプ 'Observable 'は 'Observable 'に割り当て可能な ではありません。 [0]タイプ '空'はタイプ 'ヒーロー'に割り当てられません。何か案が? – Shawn

関連する問題