#NestJS Mongoose schema type inference on required

2 messages · Page 1 of 1 (latest)

tepid dome
#

Is there a way to get the schema's type to infer if a property is required?

It feels like it needs to be specified in 2 places, eg

  @Prop({ required: true, type: String })
  name: string;
  • We have the required on the Prop
  • The name type on the 2nd line is required, eg no name?

What happens if we have the following?

  @Prop({ type: String })
  name: string;

Is there some way to infer if a prop is required directly from the type?

neon wagon
#

Is there some way to infer if a prop is required directly from the type?
Nope. Reflection (via reflect-metadata) doesn't offer info on required or optional properties.