镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Use variable declared as const as literal type #12603

Description

@Lenne231

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

Activity

  1. Lenne231 commented on Dec 1, 2016

    @Lenne231
    Author

    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?

  2. ahejlsberg commented on Dec 1, 2016

    @ahejlsberg
    Member

    This is fixed in 2.1 by #10676.

  3. 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 issue

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions