Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mrcrowl opened this issue Nov 24, 2015 · 5 comments
Closed

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

mrcrowl opened this issue Nov 24, 2015 · 5 comments
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@mrcrowl
Copy link

mrcrowl commented Nov 24, 2015

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.

@ahejlsberg
Copy link
Member

Looks like a good suggestion to me.

@RyanCavanaugh RyanCavanaugh added the In Discussion Not yet reached consensus label Nov 24, 2015
@DanielRosenwasser DanielRosenwasser added the Fixed A PR has been merged for this issue label Dec 10, 2015
@mhegazy mhegazy added this to the TypeScript 1.8 milestone Dec 10, 2015
@mhegazy mhegazy removed the In Discussion Not yet reached consensus label Dec 10, 2015
@robyoder
Copy link

It looks like this wasn't implemented as described. I can't seem to set a type guard on a getter like in the example above, only on regular functions. On a getter, I'm told "A type predicate is only allowed in return type position for functions and methods."

@mhegazy
Copy link
Contributor

mhegazy commented Mar 24, 2016

@robyoder
Copy link

I read that already, doesn't talk about this at all...

@mhegazy
Copy link
Contributor

mhegazy commented Mar 24, 2016

User define guards are not supported in property declaration or acccessor (getter or setter). that as specified in the original proposal, but was cut from the implementation.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants