8000 ts-jest users cannot use Level (const enum) when isolatedModules is enabled and diagnostic is disabled · Issue #363 · chalk/chalk · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
ts-jest users cannot use Level (const enum) when isolatedModules is enabled and diagnostic is disabled #363
Closed
@KSXGitHub

Description

@KSXGitHub

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0