Skip to main content

Use Without "emitDecoratorMetadata"

Typegoose can be used without the option emitDecoratorMetadata, but it is generally recommended to enable it for auto-inferring from the typescript type.

Advantages to "emitDecoratorMetadata"​

When using emitDecoratorMetadata, it is not needed to be explicit about everything, like the following example would simply "compile" into the appropriate types.

info

Some properties (like Arrays & Maps) need to be always explicit, see @prop Array Options.

class Kitten {
@prop({ required: true }) // Not needed to be explicit that this property is a "String"
public name!: string;

@prop({ type: () => [String], required: true })
public friendNames!: string[];

@prop({ type: () => Number, required: true })
public favoritePlacePriority!: Map<string, number>;
}

But when not using emitDecoratorMetadata, every property needs to be explicitly defined:

class Kitten {
@prop({ type: () => String, required: true }) // Needs to be explicitly defined, because "emitDecoratorMetadata" is not enabled
public name!: string;

@prop({ type: () => [String], required: true }, PropType.ARRAY)
public friendNames!: string[];

@prop({ type: () => Number, required: true }, PropType.MAP)
public favoritePlacePriority!: Map<string, number>;
}
tip

Since typegoose 12.11.0, type: [TYPE] will also infer PropType.ARRAY, unless manually specified.

Look here for what PropType is

References​