首页 文章

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

提问于
浏览
1

当“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更改为“noImplicitAny”: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'.

这是我的问题

  • 如何从订阅类型转换this._cachedHeroes.map() . subscribe()到类型Observable以解决TS2322错误?我试过 <Observable<Hero[]>>.this._cachedHeroes.... 但是没用 .

  • 如何定义TypeScript箭头函数的参数类型以解决TS2345错误?我试过 heroes.find( (hero: Hero) => {hero._id === id;}) 但它没有用 .

  • 如何在以下代码中将显式any更改为Observer类型?

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

任何建议表示赞赏 .

2 回答

  • 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() 返回 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;
    

    但我不认为这不是你的代码的意图 .

相关问题