Skip to content

require() call not suppressed when only accessing constant enumerations #4690

@sebastian-lenz

Description

@sebastian-lenz

I'm currently converting a project to use the new es6 module style (which is really a great feature!). The compiler removes all references to modules I'm using only typing information from which makes absolutely sense. However, when accessing a constant enumeration the require call will be preserved in js output although it is not needed, e.g.:

a.ts

export const enum MyEnum {
    ValueA,
    ValueB,
    ValueC
}

b.ts

import * as a from "./a";
export var myValue = a.MyEnum.ValueB;

Now, b.ts compiles to this, note the unnecessary require call:

b.js

var a = require("./a");
exports.myValue = 1;

First I thought this is my fault but then I tried to use the imported types solely as type arguments which results in the compiler removing the require call correctly, e.g.:

c.ts

import * as a from "./a";

export function myFunction(value:a.MyEnum):string {
    return "myReturn";
}

Will compile to this:

c.js

function myFunction(value) {
    return "myReturn";
}
exports.myFunction = myFunction;

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions