Skip to content

Not possible to use string-valued enums in another enum's definition #55978

Closed as not planned

Description

🔎 Search Terms

Type is not assignable to type 'number' as required for computed enum member values

🕗 Version & Regression Information

  • This changed between versions 4.9.5 and 5.0.4. Previously, this code was not flagged.

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.2.2#code/KYOwrgtgBAogjFA3gKClAglAvFA5O3AGmQF9lkBjAexAGcAXKUSW7JVWOYkgbnOegwATOzSYcA2gDp4U9N2RA

💻 Code

enum E1 {
  A = 'A',
}

const enums = {
  E1,
};

enum E2 {
  A = enums.E1.A,
}

🙁 Actual behavior

An error appears:

Type 'E' is not assignable to type 'number' as required for computed enum member values.

🙂 Expected behavior

No error should appear. Nothing in that code snippets expects numbers so this message is really confusing, especially that I'm just trying to use one string-valued enum in another one.

Additional information about the issue

Possibly related: #40793

In that issue, people are reporting similar problems, e.g.: #40793 (comment)

Activity

  1. MartinJohns commented on Oct 4, 2023

    @MartinJohns
    Contributor

    You can just do: enum E2 { A = E1.A } The issue is not trying to use the value from another enum, but the indirection via a variable.

  2. mgol commented on Oct 4, 2023

    @mgol
    Author

    Yeah, I discovered that as well. In real code, both E1 & enums are bigger and a part of a separate package so I'd need to change its API to export the enums directly.

    What's the reason for this behavior?

  3. RyanCavanaugh commented on Oct 4, 2023

    @RyanCavanaugh
    Member

    Emit can't rely on type information, and the emitter needs to know whether E2.A should be a string or numeric value (since string and numeric values have different enum emit), and the only way to do that is with type information.

  4. typescript-bot commented on Oct 7, 2023

    @typescript-bot
    Contributor

    This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  5. locked as resolved and limited conversation to collaborators on Oct 22, 2025
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

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions