question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compiler API: A reference to an enum with one value references the enum value

See original GitHub issue

Bug 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:open
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
fatcerberuscommented, Nov 10, 2021

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 as true | false. So if there’s only one member then CoolEnum and CoolEnum.Foo are in fact the same type.

0reactions
Gerrit0commented, Nov 22, 2021

That said, we do construct two separate copies of the enum type with value 1 in the single member case

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found