In our codebase we've been using T.lean()
or T.toObject()
and our return types would be LeanDocument<T>
. Mongoose 7 no longer exports LeanDocument, and the existing migration guide suggests using the following setup:
// Do this instead, no `extends Document`
interface ITest {
name?: string;
}
const Test = model<ITest>('Test', schema);
// If you need to access the hydrated document type, use the following code
type TestDocument = ReturnType<(typeof Test)['hydrate']>;
But this gives me HydratedDocument
that I can get by HydratedDocument<T>
, which is not what I want since it has all the document methods on it.
As an alternative I can use just T
as my return type, but then any Document<T>
is matching T
.
I'd like to enforce that the result is a POJO, to prevent documents leaking from our DAL.
How can I achieve that with typescript and mongoose types?