Skip to content

Arrow functions cannot be used as user-defined type guards #5951

@pimterry

Description

@pimterry

User defined type guards work perfectly well for classic function statements, as in the release notes:

interface Animal {name: string; }
interface Cat extends Animal { meow(); }

function isCat(a: Animal): a is Cat {
  return a.name === 'kitty';
}

However, it appears that the arrow function equivalent isn't acceptable:

// Doesn't compile
var isReallyCat: (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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions