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

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

Closed
mgol opened this issue Oct 4, 2023 · 4 comments
Closed

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

mgol opened this issue Oct 4, 2023 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@mgol
Copy link

mgol commented Oct 4, 2023

πŸ”Ž 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)

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

@mgol
Copy link
Author

mgol commented Oct 4, 2023

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?

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

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 4, 2023
@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants