Skip to content

Const enums are preserved when using transpileModule #5243

Closed
@mbardauskas

Description

@mbardauskas

It seems that using tsc from command line and calling ts.transpileModule in node provides different results: transpileModule preserves const enums. Whole reproduction source can be found here: https://github.com/mbardauskas/transpile-repro

file enums.d.ts:

declare const enum FooType {
    Foo = 1,
    Bar = 2,
}

file foo.ts:

/// <reference path="enums.d.ts" />

interface IFoo {
    foo: FooType;
}

class FooClass {
    public type: FooType;
    constructor(obj: IFoo) {
        this.type = obj.foo;
    }
}

export function test() {
    var foo = new FooClass(<IFoo>{
        foo: FooType.Foo
    });

    console.log(foo.type);
}

Using typescript transpileModule to compile file source

var transpiled = ts.transpileModule(inputSource, {
    reportDiagnostics: true,
    compilerOptions: {
        target: ts.ScriptTarget.ES5,
        module: ts.ModuleKind.CommonJS,
        preserveConstEnums: false
    }
});

Output from tsc:

/// <reference path="enums.d.ts" />
var FooClass = (function () {
    function FooClass(obj) {
        this.type = obj.foo;
    }
    return FooClass;
})();
function test() {
    var foo = new FooClass({
        foo: 1 /* Foo */
    });
    console.log(foo.type);
}
exports.test = test;

Output from transpileModule:

/// <reference path="enums.d.ts" />
var FooClass = (function () {
    function FooClass(obj) {
        this.type = obj.foo;
    }
    return FooClass;
})();
function test() {
    var foo = new FooClass({
        foo: FooType.Foo
    });
    console.log(foo.type);
}
exports.test = test;

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions