Skip to content

Can not build with --isolatedModules #456

Closed

Description

@neelance

Describe the Bug
Can not build with TypeScript compiler option isolatedModules.

Steps to Reproduce
Add cloudevents as a dependency to a project that uses isolatedModules and run a build.

You get:

node_modules/cloudevents/dist/index.d.ts:7:60 - error TS2748: Cannot access ambient const enums when the '--isolatedModules' flag is provided.

The problem is with export declare const enum Version. One way to solve this would be to drop the const modifier.

Expected Behavior
Build passes.

Activity

  1. github-actions commented on Jan 29, 2022

    @github-actions
    Contributor

    This issue is stale because it has been open 30 days with no activity.

  2. jketcham commented on Feb 9, 2022

    @jketcham

    I'm running into this issue as well in a Typescript project being transpiled with babel.

    For reference, it looks like there are two places where const enum is being used:

    Could a change to remove the const declaration from those enums be accepted?

    Or I think setting preserveConstEnums to true may fix the problem too, info on that option here: https://www.typescriptlang.org/tsconfig#preserveConstEnums.

    I tried both changes in a fork and all tests passed if anyone is interested in taking a look. I can open a PR if either change is reasonable.

  3. grant commented on Feb 9, 2022

    @grant
    Member

    @lance Do you think we could simply remove const from our enums here?

    Seeing a downstream issue.

  4. lance commented on Feb 9, 2022

    @lance
    Member

    @lance Do you think we could simply remove const from our enums here?

    That should be part of it. But I think the resolution should be more comprehensive. I found this article a good read on the problem, as I was unaware of exactly why this was an issue. https://ncjamieson.com/dont-export-const-enums/

    We should add "isolatedModules": "true" to our tsconfig.json. I did this locally, and it actually exposes a number of problems besides just this.

    ❯ npm run build
    
    > cloudevents@5.3.1 build /home/lanceball/src/github.com/cloudevents/sdk-javascript
    > tsc --project tsconfig.json && tsc --project tsconfig.browser.json && webpack
    
    src/index.ts:20:3 - error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
    
    20   CloudEventV1,
         ~~~~~~~~~~~~
    
    src/index.ts:21:3 - error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
    
    21   CloudEventV1Attributes,
         ~~~~~~~~~~~~~~~~~~~~~~
    
    src/index.ts:25:3 - error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
    
    25   Headers,
         ~~~~~~~
    
    src/index.ts:27:3 - error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
    
    27   Binding,
         ~~~~~~~
    
    src/index.ts:28:3 - error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
    
    28   Message,
         ~~~~~~~
    
    src/index.ts:29:3 - error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
    
    29   Deserializer,
         ~~~~~~~~~~~~
    
    src/index.ts:30:3 - error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
    
    30   Serializer,
         ~~~~~~~~~~
    
    src/index.ts:33:3 - error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
    
    33   KafkaEvent,
         ~~~~~~~~~~
    
    src/index.ts:34:3 - error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
    
    34   KafkaMessage,
         ~~~~~~~~~~~~
    
    src/index.ts:36:3 - error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
    
    36   MQTTMessage,
         ~~~~~~~~~~~
    
    src/index.ts:39:3 - error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
    
    39   TransportFunction,
         ~~~~~~~~~~~~~~~~~
    
    src/index.ts:40:3 - error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
    
    40   EmitterFunction,
         ~~~~~~~~~~~~~~~
    
    src/index.ts:43:3 - error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
    
    43   Options,
         ~~~~~~~
    
    src/message/kafka/index.ts:14:3 - error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
    
    14   KafkaMessage,
         ~~~~~~~~~~~~
    
    src/message/kafka/index.ts:15:3 - error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
    
    15   KafkaEvent
         ~~~~~~~~~~
    
    src/message/mqtt/index.ts:10:3 - error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
    
    10   MQTTMessage,
         ~~~~~~~~~~~
    
    
    Found 16 errors.
    
    npm ERR! code ELIFECYCLE
    npm ERR! errno 2
    npm ERR! cloudevents@5.3.1 build: `tsc --project tsconfig.json && tsc --project tsconfig.browser.json && webpack`
    npm ERR! Exit status 2
    npm ERR!
    npm ERR! Failed at the cloudevents@5.3.1 build script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     /home/lanceball/.npm/_logs/2022-02-09T20_02_30_244Z-debug.log
    
    
  5. lance commented on Feb 9, 2022

    @lance
    Member

    By simply changing tsconfig.json to use "isolatedModules": "true", it seems TypeScript transpiles to the correct JavaScript.

    exports.CloudEvent = exports.Version = void 0;
    const uuid_1 = require("uuid");
    
    /**
     * An enum representing the CloudEvent specification version
     */
    var Version;
    (function (Version) {
        Version["V1"] = "1.0";
        Version["V03"] = "0.3";
    })(Version = exports.Version || (exports.Version = {}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions