Compiler API: A reference to an enum with one value references the enum value
See original GitHub issueBug Report
🔎 Search Terms
single member enum, reference types, compiler API
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about symbol references
💻 Code
export enum CoolEnum {
// ^^^^^^^^ symbol.id = 103
FOO,
// ^^^ symbol.id = 104
}
export interface Response {
x: CoolEnum
// ^ type.symbol.id = 104
}
If I add a second member to the enum, the symbol references change to work as expected.
export enum CoolEnum {
// ^^^^^^^^ symbol.id = 103
FOO,
// ^^^ symbol.id = 104
BAR,
// ^^^ symbol.id = 105
}
export interface Response {
x: CoolEnum
// ^ type.symbol.id = 103
}
🙁 Actual behavior
When enums have a single value, reference types which point to them have the type’s symbol set to the only member of the enum, not to the enum’s symbol.
🙂 Expected behavior
When enums have any number of values, reference types pointing to them should point to the enum itself.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Only allow a subset of an enum as return value - Stack Overflow
How about going with approach #1 but staying safe and expressing the equivalence of the enum members? enum generic_error { ERR_OK, ERR_NOMEM ...
Read more >API reference - Better Enums - GitHub Pages
Better Enums are already represented as integers at run time. Values of the running example type Enum are the same as ints ....
Read more >Handbook - Enums - TypeScript
In this generated code, an enum is compiled into an object that stores both forward ( name -> value ) and reverse (...
Read more >Enumerations — The Swift Programming Language (Swift 5.7)
Alternatively, enumeration cases can specify associated values of any type to be stored along with each different case value, much as unions or...
Read more >TypeScript string enums, and when and how to use them
In summary, to make use of string-based enum types, we can reference them by using the name of the enum and their corresponding...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Not familiar with the compiler codebase, but my guess is this happens because an enum type is equivalent to the union of its members, similar to how
boolean
is the same type astrue | false
. So if there’s only one member thenCoolEnum
andCoolEnum.Foo
are in fact the same type.Yep… and this is why I think this is probably unintentional/buggy. I might have time to look into this… in the meantime, how should I go from a
ReferenceType
to the declaration that the user intended? Symbols seem like they ought to be the way to do this.