typescript arrow function parameter type safe

Shawn

I have code that works fine when "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.')
                                               );
  }
  .......
}        

Since I want to make it type safe, I changed the tsconfig.json to "noImplicitAny": true.

Then I got the following error

[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'.

Here are my questions

  1. How can I cast this._cachedHeroes.map().subscribe() from type Subscription to type Observable to resolve TS2322 error? I have tried <Observable<Hero[]>>.this._cachedHeroes.... but it did not work.
  2. How can I define the type for the argument to TypeScript arrow function to resolve TS2345 error? I have tried heroes.find( (hero: Hero) => {hero._id === id;}) but it didn't work.
  3. How can I change the explicit any to an Observer type in the below code?

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

Any advice is appreciated.

Shawn

With the help of @Günter Zöchbauer, I got this thing straighten out. So adding return statement will resolve the type mismatch. Here is the modified code that passed the compiler check.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Type for parameter in TypeScript Arrow Function

From Java

Specify return type in TypeScript arrow function

From Dev

Typescript: Arrow function - TS2339: Property does not exist on type '{}'

From Dev

Typescript - Arrow Function with Parameters

From Dev

Typescript Decorators and Arrow Function

From Java

TypeScript function return type based on input parameter

From Dev

typescript function parameter type unexpected token :

From Dev

Type safe parameter in method

From Dev

Type safe parameter in method

From Dev

TypeScript constructor type safe

From Dev

Typescript type guard for Type[keyof Type] function parameter

From Dev

Typescript: this inside parameters of arrow function

From Dev

How to constrain the type of generic parameter to a Typescript function to be an enum

From Dev

Typescript: function return object type with field from parameter

From Dev

Typescript error on reduce array function - "string' is not assignable to parameter of type

From Dev

Can typescript infer a function's response type based on parameter values?

From Dev

Typescript Interfaces - Implement function with narrowed union type parameter

From Dev

Typescript: function return object type with field from parameter

From Dev

Can I define a Typescript function parameter to have a type of boolean or string?

From Dev

TypeScript return the type of parameter

From Dev

Typescript multiple type parameter

From Dev

TypeScript using type parameter

From Dev

Typescript multiple type parameter

From Dev

React passing parameter with arrow function in child component

From Dev

TypeScript abstract method using lambda/arrow function

From Dev

typescript arrow operator to define a function on prototype

From Dev

Pass typescript arrow function in text (Angular)

From Dev

Safe way to call a function with a void pointer parameter

From Dev

Safe way to call a function with a void pointer parameter

Related Related

  1. 1

    Type for parameter in TypeScript Arrow Function

  2. 2

    Specify return type in TypeScript arrow function

  3. 3

    Typescript: Arrow function - TS2339: Property does not exist on type '{}'

  4. 4

    Typescript - Arrow Function with Parameters

  5. 5

    Typescript Decorators and Arrow Function

  6. 6

    TypeScript function return type based on input parameter

  7. 7

    typescript function parameter type unexpected token :

  8. 8

    Type safe parameter in method

  9. 9

    Type safe parameter in method

  10. 10

    TypeScript constructor type safe

  11. 11

    Typescript type guard for Type[keyof Type] function parameter

  12. 12

    Typescript: this inside parameters of arrow function

  13. 13

    How to constrain the type of generic parameter to a Typescript function to be an enum

  14. 14

    Typescript: function return object type with field from parameter

  15. 15

    Typescript error on reduce array function - "string' is not assignable to parameter of type

  16. 16

    Can typescript infer a function's response type based on parameter values?

  17. 17

    Typescript Interfaces - Implement function with narrowed union type parameter

  18. 18

    Typescript: function return object type with field from parameter

  19. 19

    Can I define a Typescript function parameter to have a type of boolean or string?

  20. 20

    TypeScript return the type of parameter

  21. 21

    Typescript multiple type parameter

  22. 22

    TypeScript using type parameter

  23. 23

    Typescript multiple type parameter

  24. 24

    React passing parameter with arrow function in child component

  25. 25

    TypeScript abstract method using lambda/arrow function

  26. 26

    typescript arrow operator to define a function on prototype

  27. 27

    Pass typescript arrow function in text (Angular)

  28. 28

    Safe way to call a function with a void pointer parameter

  29. 29

    Safe way to call a function with a void pointer parameter

HotTag

Archive