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

enable type guards over string literal types #6028

Closed
zpdDG4gta8XKpMCd opened this issue Dec 10, 2015 · 11 comments
Closed

enable type guards over string literal types #6028

zpdDG4gta8XKpMCd opened this issue Dec 10, 2015 · 11 comments
Labels
Committed The team has roadmapped this issue Domain: Literal Types Unit types including string literal types, numeric literal types, Boolean literals, null, undefined Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@zpdDG4gta8XKpMCd
Copy link

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

by @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.

@mhegazy mhegazy added Suggestion An idea for TypeScript Committed The team has roadmapped this issue labels Dec 10, 2015
@DanielRosenwasser
Copy link
Member

Thanks for filing @Aleksey-Bykov!

@DanielRosenwasser DanielRosenwasser added this to the TypeScript 1.8 milestone Dec 10, 2015
@DanielRosenwasser DanielRosenwasser added the Domain: Literal Types Unit types including string literal types, numeric literal types, Boolean literals, null, undefined label Jan 4, 2016
@mhegazy mhegazy removed this from the TypeScript 1.8 milestone Jan 7, 2016
@Eyas
Copy link
Contributor

Eyas commented Jan 22, 2016

Is this no longer intended for TypeScript 1.8?

@DanielRosenwasser
Copy link
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.

@Eyas
Copy link
Contributor

Eyas commented Jan 22, 2016

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

@zpdDG4gta8XKpMCd
Copy link
Author

what are the more exciting features?

@DanielRosenwasser
Copy link
Member

@Eyas Yes. 😉

@DanielRosenwasser
Copy link
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.

@zpdDG4gta8XKpMCd
Copy link
Author

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

@shaharmor
Copy link

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
}

@DanielRosenwasser
Copy link
Member

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

@zpdDG4gta8XKpMCd
Copy link
Author

yes, thanks
On Jun 23, 2016 02:16, "Daniel Rosenwasser" [email protected]
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
.

@mhegazy mhegazy added this to the TypeScript 2.0 milestone Jun 23, 2016
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jun 23, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Committed The team has roadmapped this issue Domain: Literal Types Unit types including string literal types, numeric literal types, Boolean literals, null, undefined Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants