如何解决对于打字稿,当前未启用“ decorators-legacy”的错误,事件我设置了experimentalDecorators,并且emitDecoratorMetadata为true
我将typeorm与next.js和typescript一起使用,我的tsconfig.json是:
{
"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: ...
}
我将experimentalDecorator设置为true。我的打字稿是3.9.7,我的打字稿是0.2.25。 编译错误是:
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()
我已经启用了emitDecoratorMetadata,Ho使tsconfig.json的decorator-legacy启用
解决方法
我的一个同事刚刚遇到了同样的问题:这是Babel抛出的一个错误,它无法处理类装饰器。 实际上,即使您没有在代码中使用Babel,Next.JS也会在前端使用。
确保不要在前端代码中导入./db/entity/User.ts
或任何其他TypeORM类。
如果只想导入用户类型,并使用TypeScript> = 3.8,则可以使用type-only imports来执行此操作。只需在select col1,col2,(nvl(col1,0)+nvl(col2,0))/2
关键字后面添加type
。这样可以确保仅导入类的类型,从而避免了整个装饰器问题,因为babel会忽略/删除类型。
import