Search code examples
typescriptnestjs-swagger

Class constructor SubClass cannot be invoked without 'new'


I'm currently upgrading all my dependencies and I'm facing an issue that seems to be related to @nestjs/swagger and typescript changes related to useDefineForClassFields on newer es targets.

I am facing this error, using target es2021 (also tried later ones)

TypeError: Class constructor SubClass cannot be invoked without 'new'
    at getEnumValues (repo/node_modules/@nestjs/swagger/dist/utils/enum.utils.js:7:30)
    at createApiPropertyDecorator (repo/node_modules/@nestjs/swagger/dist/decorators/api-property.decorator.js:18:59)
    at ApiProperty (repo/node_modules/@nestjs/swagger/dist/decorators/api-property.decorator.js:9:12)
    at ApiPropertyOptional (repo/node_modules/@nestjs/swagger/dist/decorators/api-property.decorator.js:43:12)

Nothing special is going on, I'm extending a class, adding a class property which itself is a class.

export class Base {
  @ApiProperty()
  start!: string

  @ApiProperty()
  end!: string
}

export class Child extends Base {
  @ApiPropertyOptional()
  subClass?: SubClass
}

I've read about typescript changes so I have some idea what is going on, but haven't been able to find a solution. How is this supposed to work in latest version?


Solution

  •   @ApiPropertyOptional({
        enum: SubClass,
        isArray: true,
        default: [],
      })
    

    Found the issue, had this incorrect code in my repo which should be type instead of enum.

      @ApiPropertyOptional({
        type: SubClass,
        isArray: true,
        default: [],
      })
    

    This was not throwing an error before upgrading so the error message was a bit misleading