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

Use variable declared as const as literal type #12603

Closed
Lenne231 opened this issue Dec 1, 2016 · 2 comments
Closed

Use variable declared as const as literal type #12603

Lenne231 opened this issue Dec 1, 2016 · 2 comments
Labels
Fixed A PR has been merged for this issue

Comments

@Lenne231
Copy link

Lenne231 commented Dec 1, 2016

It would be nice to be able to use a variable declared as const as a literal type in a type annotation.

TypeScript Version: 2.0.0

Code

export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT'
export const ADD = 'ADD';

type Action =
      { type: INCREMENT }
    | { type: DECREMENT };

Expected behavior:
No error, type Action is { type: 'INCREMENT' } | { type: 'DECREMENT' }.

Actual behavior:
Error: Cannot find name INCREMENT

@Lenne231
Copy link
Author

Lenne231 commented Dec 1, 2016

Ok,

export const INCREMENT : 'INCREMENT' = 'INCREMENT';
export const DECREMENT : 'DECREMENT' = 'DECREMENT';

type Action =
      { type: typeof INCREMENT }
    | { type: typeof DECREMENT };

seems to work, but why is const INCREMENT = 'INCREMENT'; of type string and not of type INCREMENT?

@ahejlsberg
Copy link
Member

This is fixed in 2.1 by #10676.

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Dec 1, 2016
@Lenne231 Lenne231 closed this as completed Dec 1, 2016
@mhegazy mhegazy added this to the TypeScript 2.1 milestone Dec 1, 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
Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants