如果 Nest.js 中不能使用“any”,那么 schema 中的字段类型应该是什么?

Aka*_*rma 4 javascript types mongoose typescript mongoose-schema

我正在使用 Mongoose 在 Nest.js 中工作。创建模式时,我有一个字段 extra_options 可以存储任何类型的值(数组、对象、字符串等)。将其类型保持为“any”是行不通的。正确的类型应该是什么?这是代码片段。

@Schema({
    collection: 'xyz',
    timestamps: {
        updatedAt: 'updated_at',
        createdAt: 'created_at'
    },
})
export class xyz {
    @Prop({default: true})
    active: boolean;

    @Prop()
    extra_options: any;

    @Prop({required: true})
    created_by: string;
}
Run Code Online (Sandbox Code Playgroud)

Aka*_*rma 13

import * as mongoose from 'mongoose';

@Prop({type: mongoose.Schema.Types.Mixed})
extra_options: any;
Run Code Online (Sandbox Code Playgroud)

这有效。

  • 对我有用的是: @Prop({ type: {}, default: null }) extra_options: any; (3认同)