Search code examples
typescriptmongodbmongoosenestjsmongoose-schema

NestJS + Mongoose schema with a custom typescript Type


I'm Trying to create a Mongo Schema, using nestjs/mongoose decorators, from the following class:

@Schema()
export class Constraint {
  @Prop()
  reason: string;

  @Prop()
  status: Status;

  @Prop()
  time: number;
}

The problem is Status is defined as followed:

export type Status = boolean | 'pending';

And I cannot figure out what to pass to the status's prop decorator, since I'm getting the following error:

Error: Cannot determine a type for the "Constraint.status" field (union/intersection/ambiguous type was used). Make sure your property is decorated with a "@Prop({ type: TYPE_HERE })" decorator

and { type: Status } doesn't work, since Status is a type and not a Class.


Solution

  • Since status can be a boolean or a string it's a mixed type. So you can look into setting your type to Mixed