首页 > 解决方案 > 如何修复 Typescript 无法确定类型错误

问题描述

这是我的代码 Nestjs mongoose 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”字段的类型(使用了联合/交集/模糊类型)。确保您的财产用“ @Prop({ type: TYPE_HERE })”装饰器装饰。如何解决这个问题。

标签: javascripttypescriptnestjs

解决方案


您不能声明动态类型 Like

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

Mongoose 只支持一种类型

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


推荐阅读