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

Enum from .d.ts works for value comparison but not name comparison #432

Closed
styfle opened this issue Aug 11, 2014 · 3 comments
Closed

Enum from .d.ts works for value comparison but not name comparison #432

styfle opened this issue Aug 11, 2014 · 3 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Question An issue which isn't directly actionable in code

Comments

@styfle
Copy link
Contributor

styfle commented Aug 11, 2014

I'm having trouble explaining this in words so perhaps we'll just jump straight into the code.

// The enum that was generated from the server code
declare module server {
    enum RuleType {
        All = 0,
        Required = 1,
        RequiredChoice = 2,
        EnableDisable = 3,
        EnableDisableChoice = 4,
        ShowHide = 5,
        ShowHideChoice = 6,
        AutoSet = 7,
        DependencySetup = 8,
    }
}

// Some data that we may have retreived from the server
var theItem = {value:1, name: 'Required'};

// This example works because the reference to the enum is replaced with it's value
if (theItem.value === server.RuleType.Required) {
  alert('Required checked by enum ID!');
}

// This example does not work because server.RuleType is undefined
if (theItem.name === server.RuleType[server.RuleType.Required]) {
  alert('Required checked by enum Name!');
}

At first I thought this was a problem with Web Essentials but I realized now that this would be better to fix in the TypeScript compiler since it already replaces the enum value, why not the name too?

Here is a working example if you want to see the compiled output.

@RyanCavanaugh
Copy link
Member

The compiler doesn't try to replace enum accesses with strings.

If you want to have the name<->number lookup, you can use module instead of declare module here.

@styfle
Copy link
Contributor Author

styfle commented Aug 11, 2014

@RyanCavanaugh
Yes I understand. The concern here is that there are no compiler errors so the failure happens at run time.

Also your solution does not work because removing declare says that declare modifier required for top level element

@RyanCavanaugh
Copy link
Member

You'd also need to move the enum declaration into a .ts file instead of a d.tsfile.

declare is for declaring things which exist at runtime. The compiler assumes that if you say declare var x; there's actually going to be something called x. The same applies for enums.

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

2 participants