目录

Class Constructor TypeOrmCrudService Cannot Be Invoked Without 'New'

目录

Class Constructor TypeOrmCrudService Cannot Be Invoked Without ‘New’ 错误

启因

最近开发一个chatgpt网关项目,使用nestjs,其中用nestjsx-crud来提高curd开发效率,有以下代码:

1
2
3
4
5
6
7
8
@Injectable()
export class ChatgptAccountService extends TypeOrmCrudService<ChatGPTAccount> {
  constructor(
    @InjectRepository(ChatGPTAccount)
    private readonly chatgptAccountRepository: Repository<ChatGPTAccount>,
  ) {
    super(chatgptAccountRepository);
  }

运行时会报错:

1
Class Constructor TypeOrmCrudService Cannot Be Invoked Without 'New'

解决

网上查了下,在这里找到一个: For anyone stumbling over this issue: it's probably a tsconfig issue. Try bumping your target from es5 to es6.

看一下我的项目的tsconfig.json:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
{
   "compilerOptions": {
      "lib": [
         "es5",
         "es6"
      ],
      "target": "es5",
      "module": "commonjs",
      "moduleResolution": "node",
      "outDir": "./build",
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "sourceMap": true
   }
}

再对比一下之前正常的项目:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
"compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    ...

target改成: es2017就好了:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "./build",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true
  }
}