weixin_39900437
weixin_39900437
2020-12-09 08:23

const enums not supported in some transpilers

Describe the Bug

Version appears to be undefined.

Steps to Reproduce

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

该提问来源于开源项目:cloudevents/sdk-javascript

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

8条回答

  • weixin_39900437 weixin_39900437 5月前

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

    点赞 评论 复制链接分享
  • weixin_39900437 weixin_39900437 5月前

    See https://github.com/swc-project/swc/issues/940

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

    ts
    const Versions  = {
      V03: 3,
    } as const
    
    点赞 评论 复制链接分享
  • weixin_39996096 weixin_39996096 5月前

    Hi - 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:

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

    Can you provide some steps to reproduce this?

    点赞 评论 复制链接分享
  • weixin_39900437 weixin_39900437 5月前

    Here is a repro: https://github.com/moltar/cloudevent-exp

    点赞 评论 复制链接分享
  • weixin_39996096 weixin_39996096 5月前

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

    log
     npm t
    
    > cloudevent-exp.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
    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": {
         "-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": {
    +    "/jest": "^26.0.12",
    +    "ts-jest": "^26.3.0"
       }
     }
    
    • jest.config.js
    diff
    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 -node/jest
    -  transform: {
    -    '^.+\\.(t|j)sx?$': ['-node/jest'],
    -  },
     };
    
    点赞 评论 复制链接分享
  • weixin_39900437 weixin_39900437 5月前

    It is an issue with SWC, and they did confirm it in the linked issue: https://github.com/swc-project/swc/issues/940

    But this is also a larger issue, because Babel also does not support const enums: https://github.com/babel/babel/issues/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/

    点赞 评论 复制链接分享
  • weixin_39778106 weixin_39778106 5月前

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

    点赞 评论 复制链接分享
  • weixin_39900437 weixin_39900437 5月前

    Not stale

    点赞 评论 复制链接分享

相关推荐