User defined type guards work perfectly well for classic function statements, as in the release notes:
interfaceAnimal{name: string;}interfaceCatextendsAnimal{meow();}functionisCat(a: Animal): a is Cat{returna.name==='kitty';}However, it appears that the arrow function equivalent isn't acceptable:
// Doesn't compilevarisReallyCat: (a: Animal)=>a is Cat=(cat)=>cat.name==='kitty';
This gives "Type '(cat: Animal) => boolean' is not assignable to type '(a: Animal) => a is Cat'. Signature '(cat: Animal): boolean' must have a type predicate". I'm not clear how I would give that arrow function a 'type predicate', beyond what's already included here.
Is there a good reason for this? I can't seem to find it documented anywhere.
User defined type guards work perfectly well for classic function statements, as in the release notes:
However, it appears that the arrow function equivalent isn't acceptable:
This gives "Type '(cat: Animal) => boolean' is not assignable to type '(a: Animal) => a is Cat'. Signature '(cat: Animal): boolean' must have a type predicate". I'm not clear how I would give that arrow function a 'type predicate', beyond what's already included here.
Is there a good reason for this? I can't seem to find it documented anywhere.