Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrow style user defined type guard?

The typescript handbook on user defined type guards defines an example type guard as

function isFish(pet: Fish | Bird): pet is Fish {
    return (<Fish>pet).swim !== undefined;
}

Is there a corresponding syntax for arrow functions?

like image 326
LudvigH Avatar asked Dec 16 '17 15:12

LudvigH


4 Answers

TypeScript supports arrow-function type guards (which may have not been the case in 2017). The following now works as expected. I've used this style frequently in production code:

const isFish = (pet: Fish | Bird): pet is Fish => 
  (pet as Fish).swim !== undefined;
like image 89
James Dunne Avatar answered Nov 15 '22 15:11

James Dunne


Yes, just as you would have returned boolean, you simply return pet is Fish:

const isFish: (pet: Fish | Bird) => pet is Fish = pet => (pet as Fish).swim !== undefined

Arrow functions are declared as a single type ((pet: Fish | Bird) => pet is Fish) rather than the parameters and the return type separately.

like image 32
Madara's Ghost Avatar answered Nov 15 '22 15:11

Madara's Ghost


Use type assertion instead of type declaration:

const isFish = (pet => !!pet.swim) as (pet) => pet is Fish 

However, given the fact that it is more verbose, I would prefer to write type guards as normal functions, unless you really need the this binding, but that is probably a code smell.

like image 5
unional Avatar answered Nov 15 '22 16:11

unional


as an alternative, this also works

const isFish = (pet: any): pet is Fish => !!pet.swim;

or if you prefer to be more explicit

const isFish = (pet: any): pet is Fish => pet.swim === 'function';

while the test can be made against properties as well

const isFish = (pet: any): pet is Fish => pet.hasOwnProperty('genus');
like image 2
Dan Dohotaru Avatar answered Nov 15 '22 16:11

Dan Dohotaru