TypeScript: Imported const enum is not inlined in generated code
TypeScript Version: 2.4.0
Code
// a.ts
export const enum Foo {
Bar = 'bar'
}
// b.ts
import { Foo } from './a'
Foo.Bar // TypeError or ReferenceError, depending on how it's compiled
Expected behavior:
Foo.Bar to be replaced with 'bar'
Actual behavior:
The import is deleted and Foo.Bar is left as is
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 36
- Comments: 25 (10 by maintainers)
transpileOnlymeans transpile every file one at a time… when the compiler is looking at one file, it has no way to know if the reference it is looking at is a const enum or not, since the declaration is in another file that it does not have access to…so I do not think you can mix these two concepts,
const enums(require whole program information), andtranspileOnly(one file at a time).I’ve also encountered this problem when
transpileOnly: true. WithouttranspileOnly, I can’t use multi-threading to speed up my webpack build (ts-loader, happypack, fork-ts-checker-webpack-plugin). Unfortunately, I’m dealing with +100,000 lines of legacy code, so removing all const enums is not feasible.Typescript version: 2.6.1
in my
d.tsfile I declare a const enum and use it in an interface in that same file.I use it like this in my component code (angular4): myComponent.ts:
Everything compiles but I get runtime errors since there is no LMListType. In-lining would fix this for me I think.
I could move my d.ts file to a regular ts file but then I have to import all of it’s interfaces in each component that uses them. I’d like to avoid that.
What is suggested for this issue?
@funder7 No, it hasn’t been resolved.
I encountered this bug with TypeScript 4.0.2. For me, it caused
ReferenceErrorat runtime, until I disabledisolatedModulesin mytsconfig.json.p7.zip
Version 3.8.0-dev.20200130
index.js
Enum02.Adidn’t emit as0or importtemp.tstemp.ts
index.ts
If that’s the case, then perhaps it should be an error to
exportthem to begin with (at least in non-.d.tscases). It’s not possible to make any meaningful cross-module use of them as it is.