Skip to content

preserveConstEnums does not preserve const enums from ambient declarations #1934

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

Closed
csnover opened this issue Feb 5, 2015 · 9 comments
Closed

Comments

@csnover
Copy link
Contributor

csnover commented Feb 5, 2015

// ambient.d.ts
declare module 'f' {
    export const enum A {
        A0 = 0,
        A1 = 1
    }
}

// g.ts
import f = require('f');
var a = f.A.A0;

Run: tsc -m commonjs -t es5 --preserveConstEnums ambient.d.ts g.ts

Expected output of g.js:

var f = require('f');
var a = f.A.A0;

Actual output of g.js:

var f = require('f');
var a = 0 /* A0 */;

Version: 1.4.1, 1.5.0-pre 9f49a16

@basarat
Copy link
Contributor

basarat commented Feb 5, 2015

Sorry, I should have mentioned duplicate of : #1812

@csnover
Copy link
Contributor Author

csnover commented Feb 5, 2015

Is it? That one seems like the opposite issue. Well, in any case. Here’s another test case!

@basarat
Copy link
Contributor

basarat commented Feb 5, 2015

Is it?

I am not clear either (I only know of inconsistency in the defaults used by language service vs. command line tsc : #1812 (comment))

@vladima
Copy link
Contributor

vladima commented Feb 5, 2015

preserveConstEnums flags controls if output .js file will contain definition\reverse mapping for const enums, normally these definitions are not emitted. Property accesses to members of const enums are always inlined

@basarat
Copy link
Contributor

basarat commented Feb 5, 2015

Property accesses to members of const enums are always inlined

The language service is not inlining. That is we get ts.EmitReturnStatus.Succeeded instead of 0 /* Succeeded */ from language service build output.

@csnover
Copy link
Contributor Author

csnover commented Feb 5, 2015

Sorry, I guess I totally misunderstood the purpose of this flag. I thought it was intended to keep the enum references as well. I guess then …edit this ticket into a request to have an option to do that?

Basically, at the moment, it’s impossible to create pre-compiled versions of portable libraries, since the enum values of a dependency might change across versions even though the enum keys are the same. (This happened in the TS language services from 1.4 to 1.5.)

@RyanCavanaugh
Copy link
Member

Exposing a const enum outside a compilation unit is totally inappropriate if the version-to-version key->value mapping is not guaranteed (this is a rule you'll have to enforce yourself). const enum has the same rules as a C++ enum in this regard.

The 'option' for always using property references for enum members is to not use const enums 😉

We shouldn't have broken a const enum value between versions. Which one changed specifically?

@csnover
Copy link
Contributor Author

csnover commented Feb 5, 2015

We shouldn't have broken a const enum value between versions. Which one changed specifically?

SyntaxKind

@csnover
Copy link
Contributor Author

csnover commented Feb 5, 2015

The 'option' for always using property references for enum members is to not use const enums 😉

Ugh, of course, I don’t even… some days.

@csnover csnover closed this as completed Feb 5, 2015
@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
None yet
Projects
None yet
Development

No branches or pull requests

4 participants