GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add support for "this is X" style user-defined type guards · Issue #5764 · microsoft/TypeScript

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

New issue
Copy link
New issue
Copy link
Closed
Closed
Add support for "this is X" style user-defined type guards#5764
Copy link
Labels
FixedA PR has been merged for this issueA PR has been merged for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
TypeScript 1.8

Description

@mrcrowl
mrcrowl
opened on Nov 24, 2015
Issue body actions

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 Ryan Cavanaugh (@RyanCavanaugh) when he opened #1007 but seems to be unsupported as of TypeScript 1.6.

Activity

  1. Anders Hejlsberg (ahejlsberg)
    added
    SuggestionAn idea for TypeScriptAn idea for TypeScript
    on Nov 24, 2015
  2. ahejlsberg commented on Nov 24, 2015

    @ahejlsberg
    Anders Hejlsberg (ahejlsberg)
    on Nov 24, 2015
    Member
    More actions

    Looks like a good suggestion to me.

    Reacted by Yuta Kawabe
  3. Ryan Cavanaugh (RyanCavanaugh)
    added
    In DiscussionNot yet reached consensusNot yet reached consensus
    on Nov 24, 2015
  4. Wesley Wigham (weswigham)
    closed this as completedin #5906on Dec 10, 2015
  5. Daniel Rosenwasser (DanielRosenwasser)
    added
    FixedA PR has been merged for this issueA PR has been merged for this issue
    on Dec 10, 2015
  6. Mohamed Hegazy (mhegazy)
    added this to the TypeScript 1.8 milestone on Dec 10, 2015
  7. Mohamed Hegazy (mhegazy)
    removed
    In DiscussionNot yet reached consensusNot yet reached consensus
    on Dec 10, 2015
  8. robyoder commented on Mar 24, 2016

    @robyoder
    Rob Yoder (robyoder)
    on Mar 24, 2016
    More actions

    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."

  9. mhegazy commented on Mar 24, 2016

    @mhegazy
    Mohamed Hegazy (mhegazy)
    on Mar 24, 2016
    Contributor
    More actions

    that is correct. you can find more documentation in https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in-TypeScript#this-based-type-guards

  10. robyoder commented on Mar 24, 2016

    @robyoder
    Rob Yoder (robyoder)
    on Mar 24, 2016
    More actions

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

  11. mhegazy commented on Mar 24, 2016

    @mhegazy
    Mohamed Hegazy (mhegazy)
    on Mar 24, 2016
    Contributor
    More actions

    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.

  12. microsoft
    locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueA PR has been merged for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      • TypeScript 1.8

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.