Skip to content

Add support for "this is X" style user-defined type guards #5764

Closed
@mrcrowl

Description

@mrcrowl

I'd like to use user-defined type guards in the following manner:

abstract class Animal
{
    get isDog(): this is Dog;
}

class Dog extends Animal
{
    get isDog(): this is Dog { return true; }
    bark() { console.log("woof"); }
}

let animal = new Dog();
if (animal.isDog)
{
   animal.bark()
}

This use-case was suggested by @RyanCavanaugh when he opened #1007 but seems to be unsupported as of TypeScript 1.6.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions