.ts files importing from npm module with provided .js and .ts sources do not resolve const enums
See original GitHub issue🐛 bug report
Given is the following example.ts file:
import {MyEnum} from 'my-module';
var example = MyEnum.Something;
And given is an npm module with the following files:
- my-module.d.ts
- my-module.d.ts.map
- my-module.js
- my-module.js.map
- src/my-module.ts
my-module.ts:
export const enum MyEnum { Something = 'constantString' };
The module itself is working properly.
🎛 Configuration (.babelrc, package.json, cli command)
Everything default. tsconfig.json has preserveConstEnums: false
(intentionally).
🤔 Expected Behavior
In the bundled code, the line from the example.ts should look like:
var example = "constantString";
😯 Current Behavior
Currently however, it bundles it to:
var example = MyEnum.Something;
Whereas MyEnum.Something must be resolved to the constant string behind it. Obviously, MyEnum is not existing at run time and thus gives me “undefined” errors.
💁 Possible Solution
I have tested the following:
First running tsc
and then using parcel build
on the outputted files. It worked correctly, as tsc does inline the constant enums correctly. tsc seems to process the provided .d.ts correct to use the constant strings given.
Without knowing on what exact way Parcel currently compiles TypeScript files, I would suggest tsc itself must run over the entire project, and then Parcel performs it’s other processing.
🔦 Context
I created an own private npm module exporting .js but also the original .ts files, to provide maximum compatibility for those not using TypeScript. However, now I see myself unable to use const enums from my own bundle in my TypeScript files. The only solution would be to use preserveConstEnums, which however is only a work around and no desired fix. Alternatively I could use a const variable with a set of given strings. But that also is only a work around.
🌍 Your Environment
Software | Version(s) |
---|---|
Parcel | 1.12.4 |
TypeScript | 3.6.4 |
Node | 10.16.0 |
npm/Yarn | 6.9.0 |
Operating System | Windows 10 64bit |
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (4 by maintainers)
It’s already on the todo list for the new typescript docs: https://parcel2-docs.now.sh/recipes/typescript/
I don’t think so (but be sure that tsc emits modules with ES6 imports (not CommonJS) and also set the target to the newest value to leave the JS feature transpilation to Parcel)