ts-mongoose
ts-mongoose copied to clipboard
Support for discriminators
I was trying to use ts-mongoose
for my project, but I couldn't get this to work:
import { createSchema, Type, typedModel } from 'ts-mongoose';
const discriminatorKey = 'type';
export const ThingSchema = createSchema(
{
createdBy: Type.objectId({required: true}),
isApproved: Type.boolean({required: true}),
},
{discriminatorKey},
);
export const Thing = typedModel('Thing', ThingSchema);
export const OtherThing = Thing.discriminator(
'other',
createSchema(
{
coolProperty: Type.objectId({required: true}),
},
{discriminatorKey}
),
);
async function foo() {
const f = await OtherThing.findOne();
console.log(f?.isApproved); // does not compile
}
Is there a way to get this to work I'm not seeing?