Skip to content

require() in generated JS code for a type only import holding a const enum #18644

Closed
@Flarna

Description

@Flarna

TypeScript Version: 2.5.2 / nightly (2.6.0-dev.20170921)

Code
File foo.ts exports a const enum + a function:

export const enum ConstFooEnum {
  Some,
  Values,
  Here
};
export function fooFunc(): void { /* removed */ }

File index.ts which imports Foo.ts but uses only the const enum values:

import * as Foo from "./foo";

function check(x: Foo.ConstFooEnum): void {
  switch (x) {
    case Foo.ConstFooEnum.Some:
      break;
  }
}

Expected behavior:
Generated Javascript code should not require("./foo"). Docu (http://www.typescriptlang.org/docs/handbook/modules.html) indicates that type only imports should not result in a require().

Actual behavior:
Generated JS code contains line var Foo = require("./foo"); even Foo is not used anywhere.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Foo = require("./foo");
function check(x) {
    switch (x) {
        case 0 /* Some */:
            break;
    }
}

In above simple code sample I can workaround the issue by doing importing only the ConstFooEnum via import { ConstFooEnum } from "./foo"; but if the enum is inside some namespace (e.a. enum gets re-exported from an inner module) it's not that easy.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions