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 declared expression #4150

Closed
wants to merge 3 commits into from

Conversation

jbondc
Copy link
Contributor

@jbondc jbondc commented Aug 4, 2015

When you write an enum and generate a declaration file:

enum hello {
  a,
  b,
  c = a | b,
}
tsc -d hello.ts

The output is

declare enum hello {
    a = 0,
    b = 1,
    c = 1,
}

You lose information about the intent/expression and is not useful if using definition files for documentation.

This patch preserves the original initializer expression when it resolves to a constant:

// (a)
declare enum hello {
    a = 0,
    b = 1,
    c = a | b /* 1 */,
}

Note: This conflicts with the error:

!!! error TS1066: Ambient enum elements can only have integer literal initializers.

Never really understood why that limitation exists, alternative could be to emit:

// (b)
declare enum hello {
    a = 0,
    b = 1,
    c = 1 /* a | b */,
}

Use case within compiler (typescriptServices.d.ts):

after

@msftclas
Copy link

msftclas commented Aug 4, 2015

Hi @jbondc, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!
You've already signed the contribution license agreement. Thanks!

The agreement was validated by Microsoft and real humans are currently evaluating your PR.

TTYL, MSBOT;

@DanielRosenwasser
Copy link
Member

I actually like the idea of this change, but the emit doesn't currently work for non-const enums - you get Ambient enum members can only have integer initializers.

That said, that might be the way to go - if it's a const enum, you can do this, but otherwise it might be better to refrain. That way the rule for ambient enums doesn't have to change. Maybe others have a differing opinion on this.

@jbondc
Copy link
Contributor Author

jbondc commented Aug 5, 2015

Can you explain why the error 'Ambient enum members can only have integer initializers.' was added? There's still obscure cases so I'm not sure what it's meant to enforce:

foo.ts

enum values {
    a = 9007199254740993, // Numbers allowed > safeint ?
    b = 1.1, // ok
    c = (0.1 + 0.6) + 0.3,
    d = 0.1 + (0.6 + 0.3),
}

tsc -d foo.ts

declare enum values {
    a = 9007199254740992,
    b = 1.1, // now invalid?
    c = 1,
    d = 0.9999999999999999,
}

@RyanCavanaugh
Copy link
Member

I think emitting a comment with the originating expression (option b) is far preferable here.

@DanielRosenwasser
Copy link
Member

So to clarify, with @RyanCavanaugh's suggestion, you'd get the following from your original example:

declare enum hello {
    a = 0,
    b = 1,
    c = 1 /* a | b */,
}

Though, you'd need to make sure you don't emit comments at all when doing so; you don't want something like

enum hello {
   a = 1
   b = /*hello*/ a & a
}

turning into

declare enum hello {
   a = 1
   b = 1 /*/*hello*/ a & a*/
}

@jbondc
Copy link
Contributor Author

jbondc commented Oct 8, 2015

It would be great to share the design notes on enums. I'm making efforts to get into the mindset but can't reach far preferable. Comments can easily be removed.

What I can understand is that the declaration file is more optimized the way it is now. If that's the reason, I'd prefer something like:

tsc -d foo.ts

declare enum hello {
    a = 0,
    b = 1,
    c = a | b /* 1 */,
}

tsc -d --optimize foo.ts

declare enum hello {
    a = 0,
    b = 1,
    c = 1 /* a | b */,
}

In both cases --removeComments would naturally remove the comments.

@jbondc jbondc closed this Dec 7, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants