question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.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:closed
  • Created 4 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mischniccommented, May 21, 2020

It’s already on the todo list for the new typescript docs: https://parcel2-docs.now.sh/recipes/typescript/

1reaction
mischniccommented, Oct 15, 2019

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)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Module Resolution - TypeScript
Module resolution is the process the compiler uses to figure out what an import refers to. Consider an import statement like import {...
Read more >
ts-node - npm
ts -node is a TypeScript execution engine and REPL for Node.js. It JIT transforms TypeScript into JavaScript, enabling you to directly execute ...
Read more >
How to use enum from typescript definition file? - Stack Overflow
The problem is that type definitions are just for types. They don't change the runtime behavior of a module. So while TypeScript is...
Read more >
Compiling and bundling TypeScript libraries with Webpack
If we don't provide this, npm will ignore the stuff inside .gitignore , and that's not something we want. You can find the...
Read more >
babel/preset-typescript
This is so that we know that the import is not a type import, and should not be removed. allExtensions. boolean , defaults...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found