Skip to content

enable type guards over string literal types #6028

Description

Now that the string literal types #5185 are merged in, the type guards based on them would be an awesome addition:

by Anders Hejlsberg (@ahejlsberg):

Type guards could relate the common kind property to the corresponding object type in the union and narrow the type appropriately in the guarded block.

The original issue #1003 got closed with the following comment:

the remaining part is extending the type guards to support the string literal types. but this should be tracked by a different issue.

A quick search suggests that there isn't a ticket for this yet. So here we go.

Activity

  1. DanielRosenwasser commented on Dec 10, 2015

    @DanielRosenwasser
    Member

    Thanks for filing Aleksey-Bykov!

  2. Eyas commented on Jan 22, 2016

    @Eyas
    Contributor

    Is this no longer intended for TypeScript 1.8?

  3. DanielRosenwasser commented on Jan 22, 2016

    @DanielRosenwasser
    Member

    I predict that'll be going in for 2.0. We're still working on the core functionality of string literal types, and how we want things to flow throughout the type system. If we do that right, this will be much easier and might enable much more interesting features down the road.

  4. Eyas commented on Jan 22, 2016

    @Eyas
    Contributor

    Daniel Rosenwasser (@DanielRosenwasser) is there an issue/set of issues to track about the type system work?

  5. zpdDG4gta8XKpMCd commented on Jan 22, 2016

    @zpdDG4gta8XKpMCd
    Author

    what are the more exciting features?

  6. DanielRosenwasser commented on Jan 22, 2016

    @DanielRosenwasser
    Member
  7. DanielRosenwasser commented on Jan 22, 2016

    @DanielRosenwasser
    Member

    Aleksey-Bykov basically the idea that you'd be able to not only narrow from strings to string literals, but you'd be able to use them as tags, as discussed in #1003.

  8. zpdDG4gta8XKpMCd commented on Jan 22, 2016

    @zpdDG4gta8XKpMCd
    Author

    good, that is what this issue is about, I thought there might be more to that

  9. shaharmor commented on Mar 29, 2016

    @shaharmor

    Not sure if its listed anywhere, but i think this should show an error: (Hopefully in the IDE)

    // declare
    type options = 'option1' | 'option2';
    
    interface A {
       possibleOptions: options;
    }
    
    /// use (given class B that implements interface A)
    var x = new B();
    if (x.possibleOptions === 'option3') {
      // there is no way of getting here is all possible values are either 'option1' or 'option2', should error
    }
  10. DanielRosenwasser commented on Jun 23, 2016

    @DanielRosenwasser
    Member

    Can we say that this has been fixed by #9163?

  11. zpdDG4gta8XKpMCd commented on Jun 23, 2016

    @zpdDG4gta8XKpMCd
    Author

    yes, thanks
    On Jun 23, 2016 02:16, "Daniel Rosenwasser" notifications@github.com
    wrote:

    Can we say that this has been fixed by #9163
    #9163?

    —
    You are receiving this because you were mentioned.
    Reply to this email directly, view it on GitHub
    #6028 (comment),
    or mute the thread
    https://github.com/notifications/unsubscribe/AA5PzZtK0zh11rl8vURJXL7L8VypM3hjks5qOiSfgaJpZM4GyZ6f
    .

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

    CommittedThe team has roadmapped this issueDomain: Literal TypesUnit types including string literal types, numeric literal types, Boolean literals, null, undefinedFixedA 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