Skip to content

allow constants for string literals types #6151

Description

const vow = 'will never change';
let promise: vow;

Activity

  1. RyanCavanaugh commented on Dec 18, 2015

    @RyanCavanaugh
    Member

    We can't do this since it would be a breaking change -- vow has not previously introduced an entry in the type namespace, so doing so now would mean random types would start getting shadowed.

    typeof vow should do what you want here (if we made consts be of their string literal type Daniel Rosenwasser (@DanielRosenwasser) please confirm?).

  2. zpdDG4gta8XKpMCd commented on Dec 18, 2015

    @zpdDG4gta8XKpMCd
    Author

    typeof vow should do what you want here

    in the expression context typeof vow gets me "string"
    in the type expression context it gets me "will never change", but it's a string literal type which I cannot assign to a variable

    ... if we made consts be of their string literal type

    if we assuming it's hasn't yet been done, then it answers it

  3. DanielRosenwasser commented on Dec 18, 2015

    @DanielRosenwasser
    Member

    Like Ryan Cavanaugh (@RyanCavanaugh) said, in order to do typeof vow, you'd need #5796. That said, I don't know if that PR will go in. The behavior isn't entirely ideal because it's not clear when you want the literal type and not string.

  4. mhegazy commented on Jan 15, 2016

    @mhegazy
    Contributor

    Here is the proposal: #6167 (comment)

  5. added
    SuggestionAn idea for TypeScript
    and removed
    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead
    on Jan 15, 2016
  6. sceutre commented on Nov 30, 2016

    @sceutre

    What about allowing interpolated strings as string literal types. So:

    const vow = "will never change";
    let promise: `${vow}`;
    
  7. mhegazy commented on Dec 1, 2016

    @mhegazy
    Contributor

    The issue in the OP should be fixed by #10676

    Allowing string interpolation, or any form of string manipulation in type positions is not something we are looking to allow.

    for this specific example, use typeof operator, e.g. let promise: typeof vow; and this should be "will never change".

  8. added
    FixedA PR has been merged for this issue
    and removed on Dec 1, 2016
  9. modified the milestone: on Dec 1, 2016
  10. mhegazy commented on Dec 1, 2016

    @mhegazy
    Contributor

    Aleksey-Bykov do you agree this proposal is covered by #10676 and typeof?

  11. zpdDG4gta8XKpMCd commented on Dec 1, 2016

    @zpdDG4gta8XKpMCd
    Author

    yes, it's solved, please clise

  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

    FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions