Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rxjs 6.4.0 does not compile with Create React App (@babel/plugin-transform-typescript) due to exporting a const enum #4555

Closed
BrianMitchL opened this issue Feb 10, 2019 · 7 comments

Comments

@BrianMitchL
Copy link

BrianMitchL commented Feb 10, 2019

Bug Report

Current Behavior
In RxJS 6.4.0, the NotificationKind const enum was used:

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

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';
@kwonoj
Copy link
Member

kwonoj commented Feb 10, 2019

Closing as dupe of #4538 fyi we were using string based literal and moved to enum, while we may go back to non const enum.

@kwonoj kwonoj closed this as completed Feb 10, 2019
@BrianMitchL
Copy link
Author

I was asked to open a new issue #4538 (comment)

But it really is the same root cause in the end.

@cartant
Copy link
Collaborator

cartant commented Feb 10, 2019

@BrianMitchL Your including the isolatedModules-related error makes this look like a dupe, but it might not be. If const enums not supported in the Babel transform (which makes the transform far less useful, IMO) this isn't a dupe and you will still have a problem if the exporting of the const enum in #4514 solves the isolatedModules-related error. I suspect you see that error because tsc (or something) is used for type checking before the Babel transformation.

In any case, I agree that it would be better to revert the const enum change and use a union of string-literal types.

@kwonoj
Copy link
Member

kwonoj commented Feb 10, 2019

I think this is dupe as both of then are caused by not supporting const enum, while path it causes are different: it is true babel doesn't support const enum so regardless of isolatedmodules this'll occur as soon as we use const enum though.

@kwonoj
Copy link
Member

kwonoj commented Feb 10, 2019

Which means we can't use const enum.

@BrianMitchL
Copy link
Author

BrianMitchL commented Feb 10, 2019

As I understand the CRA compile process, tsc is run for checking in your IDE, and only babel is run at compile time. babel doesn't do checking, just straight compiling, and is throws an error when encountering a const enum.

@cartant
Copy link
Collaborator

cartant commented Feb 10, 2019

@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.

@lock lock bot locked as resolved and limited conversation to collaborators Mar 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants