Skip to content

Unable to compile const enum in .d.ts file. #706

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
zheeeng opened this issue Jan 10, 2018 · 11 comments
Closed

Unable to compile const enum in .d.ts file. #706

zheeeng opened this issue Jan 10, 2018 · 11 comments

Comments

@zheeeng
Copy link

zheeeng commented Jan 10, 2018

I'm using .d.ts to store some enum interfaces. But it seems that ts-loader doesn't include these .d.ts files in compiling.

A work situation:

// foo.ts
const enum Direction {
    Up,
    Down,
    Left,
    Right,
}

const d: Direction = Direction.Up

output:

var d = 0 /* Up */;

For reusing Direction as value and interface, I separated declaration and its applying. Unfortunately, the compiling failed.

// foo.d.ts
declare const enum Direction {
    Up,
    Down,
    Left,
    Right,
}
// foo.ts
const d: Direction = Direction.Up

output:

var d = Direction.Up

This way report ReferenceError: Direction is not defined in webpack compliing result.

@KnisterPeter
Copy link
Contributor

KnisterPeter commented Jan 28, 2018

@zheeeng Couldn't you just export your const enum from 'foo.ts'?
Otherwise you may use a tripple-slash-directive to reference your 'foo.d.ts' which will instruct the compiler to include your file.

@zheeeng
Copy link
Author

zheeeng commented Jan 28, 2018

@KnisterPeter

const enum is handy for number semantic. declare is handy for using in global. The more important, typescript allow us to use them in that way.
export means we have to import it everywhere. And the behaviour difference between native typescript compiled and the transpiled brings a little inconvenience.

@KnisterPeter
Copy link
Contributor

@zheeeng Fair engouth. Does it work with plain tsc? I doubt this is a ts-loader issue, since ts-loader just calls tsc from webpack (but I'm not sure, could be some side-effect).

@KnisterPeter
Copy link
Contributor

I think you need to add a tripple-slash somewhere in your source like this:

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

This is a way to tell the tsc to include your declaration in your compilation.
As far as I know you can either use import/export or tripple-slash references to include files in your compiler context.

@zheeeng
Copy link
Author

zheeeng commented Jan 28, 2018

@KnisterPeter tsc works fine. I think it is something caused by webpack only consider files things are imported. Declaration files don't need to be imported.
If it is the reason. I doubt tripple-slash import works.
Certainly, webpack won't take this issue. Maybe some improvements on ts-loader will help.

@KnisterPeter
Copy link
Contributor

@zheeeng Please add a minimal repository to test this issue. It may help to understand it a bit more.

@zheeeng
Copy link
Author

zheeeng commented Jan 28, 2018

Ok, I try build one.

@zheeeng
Copy link
Author

zheeeng commented Jan 28, 2018

@KnisterPeter very sorry for my mistakes. I just then build a minimal project for test, if the declaration file is included by 'include' option tsconfig.json. It indeed is transpiled right.
The issue was created when I was developing project with complicated configs, VS Code didn't report error while the const enum declaration is out of the include range that be defined by tsconfig.json.

@zheeeng zheeeng closed this as completed Jan 28, 2018
@KnisterPeter
Copy link
Contributor

Your welcome. I'm glad to help.

@jestarray
Copy link

Having this issue currently with the Vue CLI that uses webpack and ts-loader under the scenes.

vuejs/vue-cli#3376

tried flipping transpileOnly on and off, and happymodepack on and off, no luck.

@navneet-g
Copy link

Is there a way to generate a build error when we use enum instead of const enum in d.ts files?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants