Skip to content

const enums not supported in some transpilers #323

New issue

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

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

Already on GitHub? # to your account

Closed
moltar opened this issue Aug 21, 2020 · 12 comments · Fixed by #561
Closed

const enums not supported in some transpilers #323

moltar opened this issue Aug 21, 2020 · 12 comments · Fixed by #561

Comments

@moltar
Copy link

moltar commented Aug 21, 2020

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.

@moltar
Copy link
Author

moltar commented Aug 21, 2020

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

@moltar
Copy link
Author

moltar commented Aug 21, 2020

See swc-project/swc#940

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

const Versions  = {
  V03: 3,
} as const

@moltar moltar reopened this Aug 25, 2020
@lance lance changed the title TypeError: Cannot read property 'V03' of undefined const enums not supported in some transpilers Aug 26, 2020
@lance
Copy link
Member

lance commented Aug 26, 2020

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?

@moltar
Copy link
Author

moltar commented Aug 28, 2020

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

@lance
Copy link
Member

lance commented Sep 1, 2020

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'],
-  },
 };

@moltar
Copy link
Author

moltar commented Sep 3, 2020

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/

@github-actions
Copy link
Contributor

github-actions bot commented Oct 4, 2020

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

@moltar
Copy link
Author

moltar commented Oct 4, 2020

Not stale

@github-actions
Copy link
Contributor

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

@moltar
Copy link
Author

moltar commented Feb 23, 2021

@lance
Copy link
Member

lance commented Mar 3, 2021

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

@github-actions
Copy link
Contributor

github-actions bot commented Apr 3, 2021

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants