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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Gerrit0 opened this issue Nov 10, 2021 · 6 comments
Open
Labels
API Relates to the public API for TypeScript Experimentation Needed Someone needs to try this out to see what happens Suggestion An idea for TypeScript

Comments

@Gerrit0
Copy link
Contributor

Gerrit0 commented Nov 10, 2021

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.

@fatcerberus
Copy link

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.

@andrewbranch
Copy link
Member

That鈥檚 correct

@andrewbranch
Copy link
Member

That said, we do construct two separate copies of the enum type with value 1 in the single member case, one returned for getTypeOfSymbol for the enum declaration symbol and one for the member declaration symbol. This is a little weird.

@andrewbranch
Copy link
Member

@Gerrit0 you can try a draft PR that changes this; sometimes we just don鈥檛 have great coverage/visibility about what the public checker API returns. If it doesn鈥檛 cause any breaks/changes internally we might consider it, but no guarantees.

@andrewbranch andrewbranch added API Relates to the public API for TypeScript Experimentation Needed Someone needs to try this out to see what happens Suggestion An idea for TypeScript labels Nov 10, 2021
@Gerrit0
Copy link
Contributor Author

Gerrit0 commented 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.

@adamviola
Copy link

adamviola commented Jun 20, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Relates to the public API for TypeScript Experimentation Needed Someone needs to try this out to see what happens Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants