如何修复 Typescript CannotDetermineTypeError

How to fix Typescript CannotDetermineTypeError

这是我的代码 Nestjs 猫鼬 test.schema.ts

@Schema({ timestamps: true })
export class Tests {
  @Prop({ required: true, index: true })
  testid: number;

  @Prop({ required: false })
  extra_id: string | number;

  @Prop({ required: false })
  points: number;
}

为此,我遇到了以下错误, CannotDetermineTypeError:无法确定“Tests.extra_id”字段的类型(使用了 union/intersection/ambiguous 类型)。确保您的 属性 装饰有“@Prop({ type: TYPE_HERE })”装饰器。 如何解决这个问题。

您不能像这样声明动态类型

 @Prop({ required: false })
 extra_id: string | number;

Mongoose 只支持一种类型

@Prop({ type : String, required: false })
     extra_id: string;