Closed
Description
Problem
Let's say we have a TS file:
// index.ts
import { Level } from 'chalk'
console.log(Level.Basic)
Use tsc
to try compile it with --isolatedModules
:
tsc --isolatedModules --module commonjs --esModuleInterop --target es2018 index.ts
This should print an error:
error TS2748: Cannot access ambient const enums when the '--isolatedModules' flag is provided.
However, when using ts-jest
with diagnostic: false
(ignore all TS errors), it emits a JavaScript code equivalent to:
const { Level } = __importDefault(require('chalk')) // Level = undefined
console.log(Level.Basic) // TypeError: Cannot read property 'Basic' of undefined
Suggestions
This can be fixed by doing either:
1. Replace the const enum LevelEnum
with a combination of type union and namespace
type Level = 0 | 1 | 2 | 3
This would be a breaking change as TS users would no longer be able to use Level.Basic
in place of 1
like they used to.
2. Preserve the const enum, and add Level
object to the JavaScript file.
module.exports.Level = {
None: 0,
Basic: 1,
Ansi256: 2,
TrueColor: 3
}
This would not be a breaking change.