ts-mongoose icon indicating copy to clipboard operation
ts-mongoose copied to clipboard

Support for discriminators

Open talzion12 opened this issue 4 years ago • 0 comments

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?

talzion12 avatar Feb 16 '20 17:02 talzion12