Home > Software design >  How to fix Typescript CannotDetermineTypeError
How to fix Typescript CannotDetermineTypeError

Time:10-06

Here is my code 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;
}

for this, I'm getting below error, CannotDetermineTypeError: Cannot determine a type for the "Tests.extra_id" field (union/intersection/ambiguous type was used). Make sure your property is decorated with a "@Prop({ type: TYPE_HERE })" decorator. How to solve this.

CodePudding user response:

You cannot declare Dynamic Type Like

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

Mongoose support just One Type Like

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

  • Related