for typescript the error for 'decorators-legacy' isn't currently enabled, event I set experimentalDecorators and emitDecoratorMetadata is true

Viewed 1902

I use typeorm with next.js and typescript, my tsconfig.json is:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext",
      "es5",
      "es6"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "jsx": "preserve"
  },
   include: ...

}

I set experimentalDecorator is true. My typescript is 3.9.7 and my typeorm is 0.2.25. The compile error is:

error - ./db/entity/User.ts:7:1
Syntax error: Support for the experimental syntax 'decorators-legacy' isn't currently enabled:

   5 |
   6 |
>  7 | @Entity()
     | ^
   8 | export class User {
   9 |
  10 |     @PrimaryGeneratedColumn()

I already enable emitDecoratorMetadata, Hoe to make decorators-legacy enable for tsconfig.json

2 Answers

A coworker of mine has just faced the same problem: this is an error throwed by Babel which can't handle class decorators. In fact, even if you don't use Babel in your code, Next.JS does in the frontend.

Be sure you don't import ./db/entity/User.ts or any other TypeORM class in your frontend code.

If you only want to import the User type, and use TypeScript >= 3.8, you might be able to do this using type-only imports. Just add type after the import-keyword. This ensures that only the type of the class is imported, thus avoiding the whole decorator problem since types are ignored/removed by babel.

import type { User } from "path/to/db/entity/User";
Related