Repository: ReactiveX/rxjs

Issue #4538: Ambient const enums are not allowed when the '--isolatedModules' flag is provided (with typescript)
Created: 2019-02-07T12:31:08Z
Bug Report
Current Behavior
When compiling a typescript file that imports something from rxjs with the isolatedModules option for typescript set to true, the following error occurs:
node_modules/rxjs/internal/Notification.d.ts:3:27 - error TS1209: Ambient const enums are not allowed when the '--isolatedModules' flag is provided.

3 export declare const enum NotificationKind {

Reproduction
Create minimal project
yarn add --dev typescript rxjs
ts 
Create test.ts:
import {Observable} from 'rxjs'
Run Typescript compiler
yarn tsc --lib esnext --isolatedModules test.ts
Expected behavior
No error.
Environment

Runtime: Node v10.15.0
RxJS version: 6.4.0
(If bug is related) Loader, build configuration: typescript 3.3.1

Possible Solution

Additional context/Screenshots
Add any other context about the problem here. If applicable, add screenshots to help explain.

Duplicate issue #4555: rxjs 6.4.0 does not compile with Create React App (@babel/plugin-transform-typescript) due to exporting a const enum
Created: 2019-02-10T19:02:07Z
Bug Report
Current Behavior
In RxJS 6.4.0, the NotificationKind const enum was used:

  
    
      rxjs/src/internal/Notification.ts
    
    
         Line 7
      in
      6c4f57b
    
  
  
    

        
          
           export const enum NotificationKind { 
        
    
  


Unfortunately, babel does not support const enums, so any application that is compiled with it will not compile. This is the TypeScript compiler used in create-react-app apps. The errors presents itself when the TypeScript skipLibCheck flag is set to false.
Type error: Ambient const enums are not allowed when the '--isolatedModules' flag is provided.  TS1209

    1 | import { PartialObserver } from './types';
    2 | import { Observable } from './Observable';
  > 3 | export declare const enum NotificationKind {
      |                           ^
    4 |     NEXT = "N",
    5 |     ERROR = "E",
    6 |     COMPLETE = "C" 

Reproduction

REPL or Repo link:
https://github.com/BrianMitchL/rxjs-cra-compile-error

The 6.4.0 branch errors when running npm run start
when the 6.3.3 branch works.
Expected behavior
I would expect the application to compile with RxJS 6.4.0 when using the babel compiler.
Environment

Runtime: Tested with Node v10.15.0 and v11.9.0
RxJS version: 6.4.0
Other related libraries:
typescript@3.3.3
react-scripts@2.1.3

Possible Solution
Removing the const from the enum. From the babel docs:

Does not support const enums because those require type information to compile. Workaround: Remove the const, which makes it available at runtime.

Otherwise, this might be a good place to use a union string type. Instead of
export const enum NotificationKind {
  NEXT = 'N',
  ERROR = 'E',
  COMPLETE = 'C',
}
the following would be used
export type NotificationKind = 'NEXT' | 'ERROR' | 'COMPLETE';

Closing comment (2019-02-10T22:05:28Z): @kwonoj

Closing as dupe of #4538

Yeah. You are completely correct. The use of the term 'ambient' in the error message confused me - as the term has a different meaning for .d.ts files.
It's clear to me now that the problem stems from the fact that when transpiling a single file, it's not possible to know which literal value should be used to replace a reference to a const enum.
I'll add a script that runs a transpilation (with no output and with isolatedModules set to true) when Travis performs its full validation. And I'll replace the const enum with a string-literal union type.