Skip to content

const enums not supported in some transpilers #323

Closed

Description

@moltar

Describe the Bug

Version appears to be undefined.

Steps to Reproduce

import { CloudEvent, Version } from 'cloudevents'

new CloudEvent({
      id: event.id.toString(),
      source: event.source,
      type: event.type,
      subject: event.subject,
      time: event.time,
      data: event.data,
      specversion: Version.V03,
})

Expected Behavior

Version should be defined.

Additional context

Using package from NPM version 3.1.0.

Activity

  1. moltar commented on Aug 21, 2020

    @moltar
    Author

    Never mind, seems to be an issue with @swc-node/jest.

  2. moltar commented on Aug 21, 2020

    @moltar
    Author

    See swc-project/swc#940

    Maybe this can refactored from enum to const as const approach?

    const Versions  = {
      V03: 3,
    } as const
  3. reopened this on Aug 25, 2020
  4. changed the title [-]TypeError: Cannot read property 'V03' of undefined[/-] [+]const enums not supported in some transpilers[/+] on Aug 26, 2020
  5. lance commented on Aug 26, 2020

    @lance
    Member

    Hi @moltar - I am unable to reproduce this. I tried downloading the swc CLI, and ran it on src/event/cloudevent.ts where the Version enum is declared, and did not get any errors. It produced this transpiled JS:

    export var Version;
    (function(Version1) {
        Version1["V1"] = "1.0";
        Version1["V03"] = "0.3";
    })(Version || (Version = {
    }));

    Can you provide some steps to reproduce this?

  6. moltar commented on Aug 28, 2020

    @moltar
    Author
  7. lance commented on Sep 1, 2020

    @lance
    Member

    Thanks @moltar. This really seems to be an issue with swc. By modifying your example to use ts-jest instead of swc the example runs without error.

     npm t
    
    > cloudevent-exp@1.0.0 test /home/lanceball/src/github.com/tmp/cloudevent-exp
    > jest
    
     PASS  ./cloudevents.test.ts
      Version
        ✓ should work (2 ms)
    
    Test Suites: 1 passed, 1 total
    Tests:       1 passed, 1 total
    Snapshots:   0 total
    Time:        2.39 s
    Ran all test suites.
    

    My modifications.

    • package.json
    diff --git a/package.json b/package.json
    index 2342abf..fa3a0b7 100644
    --- a/package.json
    +++ b/package.json
    @@ -11,10 +11,12 @@
       "license": "ISC",
       "dependencies": {
         "@swc-node/jest": "0.1.27",
    -    "cloudevent": "0.6.0",
         "cloudevents": "3.1.0",
         "jest": "26.4.2",
    -    "ts-jest": "26.3.0",
    -    "typescript": "4.0.2"
    +    "typescript": "^4.0.2"
    +  },
    +  "devDependencies": {
    +    "@types/jest": "^26.0.12",
    +    "ts-jest": "^26.3.0"
       }
     }
    • jest.config.js
    diff --git a/jest.config.js b/jest.config.js
    index 0bda6ad..91a2d2c 100644
    --- a/jest.config.js
    +++ b/jest.config.js
    @@ -1,11 +1,4 @@
     module.exports = {
    +  preset: 'ts-jest',
       testEnvironment: 'node',
    -
    -  // config for ts-jest
    -  // preset: 'ts-jest',
    -
    -  // config for @swc-node/jest
    -  transform: {
    -    '^.+\\.(t|j)sx?$': ['@swc-node/jest'],
    -  },
     };
  8. moltar commented on Sep 3, 2020

    @moltar
    Author

    It is an issue with SWC, and they did confirm it in the linked issue: swc-project/swc#940

    But this is also a larger issue, because Babel also does not support const enums: babel/babel#8741

    I don't use babel, but there is a large portion of TS users who do.

    Here is a short explainer (not my article): https://ncjamieson.com/dont-export-const-enums/

  9. github-actions commented on Oct 4, 2020

    @github-actions
    Contributor

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

  10. moltar commented on Oct 4, 2020

    @moltar
    Author

    Not stale

  11. github-actions commented on Feb 23, 2021

    @github-actions
    Contributor

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

  12. lance commented on Mar 3, 2021

    @lance
    Member

    @moltar I see your point to some degree (though the blog post you linked is 404), which is why we have modified the bot to never actually close an issue. But having the label is useful, imo.

  13. github-actions commented on Apr 3, 2021

    @github-actions
    Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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