TypeScript/tests/cases/compiler/isolatedModulesAmbientConstEnum.ts
Alan Pierce 293eba6203 Change isolatedModules to allow const enum declaration and disallow access
Fixes #20703 with solution suggested in https://github.com/Microsoft/TypeScript/issues/20703#issuecomment-361434795

Previously, `--isolatedModules` gave an error for any ambient const enum, which
meant that some third-party libraries would always give errors even if the
ambient const enums they declare were never used. Now, we only give an error
when an ambient const enum is referenced, which allows such libraries to still
be used as long as the const enums are never accessed.

Some nuances:
* As before, the error is only surfaced for *ambient* const enums. With
  non-ambient const enums, we know that an `isolatedModules` build will emit the
  enum and produce a plain reference rather than inlining the constant, so
  everything will still work.
* I originally planned to do this check in the code path that inlines the
  constant, but that code is only exercised at emit time, so, for example, the
  TS language service wasn't giving an error in my editor. Instead, I do the
  check at typecheck time next to another const-enum-related check.
* This can be a breaking change when using `skipLibCheck` because the error is
  typically moved from a .d.ts file to a .ts file.

Testing done:
I ran this TS build on a large project of mine that previously had disabled
`isolatedModules` so I could use the `chalk` library. With `isolatedModules`
enabled, there was no longer an error in the chalk typedefs, and a reference to
the `Level` const enum produced an error in my editor.
2018-11-10 14:47:05 -08:00

8 lines
124 B
TypeScript

// @isolatedModules: true
// @target: es6
// @filename: file1.ts
declare const enum E { X = 1}
export var y = E.X;