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.

isolatedModules doesn't respect enabled preserveConstEnum option what the project might be build with

See original GitHub issue

Search Terms

isolatedModules, preserveConstEnum, const enum

Suggestion

The compiler should respect/understand that the package/module was compiled with enabled preserveConstEnums compiler option.

Use Cases

Let’s say we have a project written in TypeScript, which uses const enums.

The project uses them because the main const enum’s advantage is inlining values instead of runtime accessing to the value.

In other hand the maintainers understand that const enums compiles in nothing in JS code, and that means that consumers can’t use a const enums in their JS code (in some cases in TS code either, see above). Thus, they decided to enable preserveConstEnums compiler option to leave all const enums in JS code and allow consumers use them as well.

Up to this moment everything is good. But the next who appears on the stage is isolatedModules (say hello to create-react-app, webpack’s transpile-only mode, transpileModule compiler’s API and so on). This compiler option is used to detect that imported module could be transpiler without compilation into JS code and everything will work well. One of checks for this option is that you don’t import const enums (error TS2748: Cannot access ambient const enums when the '--isolatedModules' flag is provided).

To summarize, if the project/package is compiled with enabled preserveConstEnums, in isolatedModules mode the compiler shouldn’t fail with error about unavailability of using const enums, because that’s not actually true.

Examples

Quite possible we need to provide that information to the compiler somehow (by providing tsconfig.export.json or in package.json for instance), but I don’t even know how it would be done.

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
andrewbranchcommented, Aug 20, 2021

We’ve discussed this some in the past and I feel like I must be forgetting some of the complexities that came up. I just jotted down some straw-man proposals, and the simplest one feels fairly straightforward:

preserved modifier for const enums

// @Filename: /node_modules/foo/index.d.ts
export preserved const enum Foo {
  Bar = 1
}

// @Filename: /index.ts
// @isolatedModules: true
import { Foo } from "foo";
Foo.Bar; // ok
  • Only valid in ambient contexts, automatically added to declaration emit under --preserveConstEnums.
  • A preserved const enum can be imported and used under --isolatedModules because its usage need not be inlined.
  • If needed, a compiler flag could be added that controls whether preserved const enums from node_modules / other projects / declaration files get inlined or not. (We’ve had some concerns before about inlining enum values from declaration files that could get out of sync with implementations, but I want to hear the case for that again before I’d be enthusiastic about adding a flag.)

I tossed around another proposal combining @RyanCavanaugh’s original suggestion that const enums could be emitted to declaration files as regular enums with a compiler option --inlineEnums that could offer local control over what enums get inlined, but ultimately I think that level of control would be more confusing than useful. Part of our hesitation on doing anything about this is that enums are already confusing, so we want a solution to be as simple and explainable as possible.

2reactions
RyanCavanaughcommented, Apr 3, 2020

It seems like arguably preserveConstEnum should just emit enum instead of const enum to the .d.ts, though I’m sure that would break some other scenario

Read more comments on GitHub >

github_iconTop Results From Across the Web

isolatedModules - TSConfig Option - TypeScript
Setting the isolatedModules flag tells TypeScript to warn you if you write certain code that can't be correctly interpreted by a single-file transpilation ......
Read more >
Babel 7 disable --isolatedModules Typescript flag?
I've tried disabling it my tsconfig but I think babel is ignoring that. 'use strict'; // Source maps are resource heavy and can...
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 >
Isolated Modules option | ts-jest - GitHub Pages
By default ts-jest uses TypeScript compiler in the context of a project (yours), with full type-checking and features.
Read more >
Help prompts for different compilers
-b, --build Build one or more projects and their dependencies, if out of date -t VERSION, ... --strict Enable all strict type-checking options....
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