Nodejs: Invalid or unexpected token while using decorators typescript

1.4k Views Asked by At

I'm was using type-graphql to create a simple API. So I created a simple query which is working fine before.

@Resolver()
class B {
  @Query({nullable: true})
  async a(@Arg("b", () => String) b: string) {

  }
}

But when I integrated babel in my project it's start throwing this error:

/Users/robot/Desktop/node-test/src/index.ts:37
      var _a = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(@(0, _typeGraphql.Arg)("b", function () {
                                                                                                        ^

SyntaxError: Invalid or unexpected token

.babelrc:

{
  "presets": [
    "@babel/preset-env", 
    "@babel/preset-typescript"],
  "plugins": [
    ["@babel/plugin-proposal-decorators", {"legacy": true}],
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-transform-typescript",
    "@babel/plugin-transform-runtime"
  ]
}
1

There are 1 best solutions below

0
On

Try removing {"legacy": true} from your .babelrc unless you are sure that you need it. If you do need it, using {"legacy": true} with @babel/plugin-proposal-decorators needs some additional configuration according to https://babeljs.io/docs/en/babel-plugin-proposal-decorators#legacy :

When using the legacy: true mode, the setPublicClassFields assumption must be enabled to support the @babel/plugin-proposal-decorators.

The example configuration they give is

{
  "assumptions": {
    "setPublicClassFields": true
  },
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-proposal-class-properties"]
  ]
}

Applying that to example to your .babelrc would change it to the following. Does that solve your issue?

{
  "assumptions": {
    "setPublicClassFields": true
  },
  "presets": [
    "@babel/preset-env", 
    "@babel/preset-typescript"],
  "plugins": [
    ["@babel/plugin-proposal-decorators", {"legacy": true}],
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-transform-typescript",
    "@babel/plugin-transform-runtime"
  ]
}