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

Typescript enum编译错误“enum is a reserved word” #396

Closed
eyeye opened this issue Jul 28, 2018 · 5 comments
Closed

Typescript enum编译错误“enum is a reserved word” #396

eyeye opened this issue Jul 28, 2018 · 5 comments

Comments

@eyeye
Copy link

eyeye commented Jul 28, 2018

问题描述
创建typescript项目,定义enum编译失败:SyntaxError: unknown: enum is a reserved word,似乎不支持enum?

复现步骤

export const ADD = 'ADD'
export const MINUS = 'MINUS'
enum Directions {
    Up,
    Down,
    Left,
    Right
}

let directions = [Directions.Up, Directions.Down, Directions.Left, Directions.Right];

报错信息

错误  编译失败  src\constants\counter.ts
{ SyntaxError: unknown: enum is a reserved word (4:0)
  2 | export const ADD = 'ADD';
  3 | export const MINUS = 'MINUS';
> 4 | enum Directions {
    | ^
  5 |   Up,
  6 |   Down,
  7 |   Left,
    at Parser.pp$5.raise (E:\workspace\Taro\demo0\node_modules\babylon\lib\index.js:4454:13)
    at Parser.pp$3.checkReservedWord (E:\workspace\Taro\demo0\node_modules\babylon\lib\index.js:4347:10)
    at Parser.pp$3.parseIdentifier (E:\workspace\Taro\demo0\node_modules\babylon\lib\index.js:4324:10)
    at Parser.pp$3.parseExprAtom (E:\workspace\Taro\demo0\node_modules\babylon\lib\index.js:3648:21)
    at Parser.parseExprAtom (E:\workspace\Taro\demo0\node_modules\babylon\lib\index.js:7238:22)
    at Parser.pp$3.parseExprSubscripts (E:\workspace\Taro\demo0\node_modules\babylon\lib\index.js:3494:19)
    at Parser.pp$3.parseMaybeUnary (E:\workspace\Taro\demo0\node_modules\babylon\lib\index.js:3474:19)
    at Parser.pp$3.parseExprOps (E:\workspace\Taro\demo0\node_modules\babylon\lib\index.js:3404:19)
    at Parser.pp$3.parseMaybeConditional (E:\workspace\Taro\demo0\node_modules\babylon\lib\index.js:3381:19)
    at Parser.pp$3.parseMaybeAssign (E:\workspace\Taro\demo0\node_modules\babylon\lib\index.js:3344:19)
  pos: 97,
  loc: Position { line: 4, column: 0 },
  _babel: true,
  codeFrame:
   '\u001b[0m \u001b[90m 2 | \u001b[39m\u001b[36mexport\u001b[39m \u001b[36mconst\u001b[39m \u001b[33mADD\u001b[39m \u001b[33m=\u001b[39m \u001b[32m\'ADD\'\u001b[39m\u001b[33m;\u001b[39m\n \u001b[90m 3 | \u001b[39m\u001b[36mexport\u001b[39m \u001b[36mconst\u001b[39m \u001b[33mMINUS\u001b[39m \u001b[33m=\u001b[39m \u001b[32m\'MINUS\'\u001b[39m\u001b[33m;\u001b[39m\n\u001b[31m\u001b[1m>\u001b[22m\u001b[39m\u001b[90m 4 | \u001b[39m\u001b[36menum\u001b[39m \u001b[33mDirections\u001b[39m {\n \u001b[90m   | \u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\n \u001b[90m 5
| \u001b[39m  \u001b[33mUp\u001b[39m\u001b[33m,\u001b[39m\n \u001b[90m 6 | \u001b[39m  \u001b[33mDown\u001b[39m\u001b[33m,\u001b[39m\n \u001b[90m 7 | \u001b[39m  \u001b[33mLeft\u001b[39m\u001b[33m,\u001b[39m\u001b[0m' }

系统信息

  • 操作系统: [Windows 10]
  • Taro 版本 [v0.0.73]
  • Node.js 版本 [v10.7.0]
@yuche
Copy link
Contributor

yuche commented Jul 29, 2018

这是 babel-typescript-plugin 的问题。现在 babel-typescript-plugin 不能使用 export =, enumnamespace

ref:https://babeljs.io/docs/en/next/babel-plugin-transform-typescript.html

ts.transpile 能解决这个问题但有新的问题:

  1. ts.transpile一定会把装饰器编译成一个函数,并且该行为无法更改。原因在于ECMAScript 装饰器提案还不稳定,和 TypeScript 的装饰器行为有些不一致(ref:Add compiler options to disable decorator transform microsoft/TypeScript#16882 )。而我们希望他不编译装饰器,保持 connect 函数原有的样子 taro-redux 才能跑起来。
  2. 一些情况无法解析正确 JSX。

还得再想想怎么做。

@eyeye
Copy link
Author

eyeye commented Jul 30, 2018

@yuche 好的,谢谢答复! 我先将就着不用 enum吧。

@yuche yuche closed this as completed in 61d3e24 Aug 1, 2018
@Y80
Copy link

Y80 commented Jun 13, 2021

在 eslint 配置文件中添加"plugins": ["const-enum"]可以解决问题!

@chwan97
Copy link

chwan97 commented Aug 9, 2021

在 eslint 配置文件中添加"plugins": ["const-enum"]可以解决问题!

没用,能讲具体一点吗,多谢

@shungang
Copy link

plugins: ['typescript-enum']
这个可以

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants