Replies: 1 comment 1 reply
-
Hard to give an exact answer without more info, but I think the general idea is that you'll need a function that takes a generic parameter and returns a model. Something like the following. import mongoose from 'mongoose';
type IData<T> = {
id: number,
name: string,
data: T
}
function Foo<T>(type: Function) {
const DataSchema = new mongoose.Schema<IData<T>>({
id: Number,
name: String,
data: type
});
return mongoose.model('Data', DataSchema);
}
const Test = Foo<String>(String);
const doc = new Test({ data: 42 });
console.log(doc); Does that help? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi
I'm trying to implement something like this
Basically, a schema that one of its fields is generic.
I'm aware that I can use
Schema.Types.Mixed
for the "data" property, but is there any way to keep the typescript generic feature? Or any better way to implement it?Beta Was this translation helpful? Give feedback.
All reactions