I've created a codesandbox for easier trial. But below are also the parts that are more related to my question.
This is the type that is expected to extract the raw Mongoose document's type (It doesn't.):
// ⭐ class Document<T = any, TQueryHelpers = any, DocType = any>
type ObtainRawDocType<T> = T extends mongoose.Document<
infer A,
infer B,
infer C
>
? C // <--- ⭐ Must return the raw document's type.
: never;
This is where the above generic is used (The return type is not right on purpose, in order to test the above generic.):
export function clearFields<
T extends mongoose.Document,
U extends PropertyKey[]
>(dataObj: T, privateFields: U): ObtainRawDocType<T> {
However, this cc1's type is detected to be unknown:
const cc1 = clearFields(c1, ["birthdate"]);
this question was also asked on the typegoose discord, where we came to the conclusion that the resulting
unknownis because typescript discards the original type if it has been AND'd with aOmittype and is not structurally used anywhere.typescript playground example of the problem
the answer is basically to augment the
mongoose.Documenttype to structurally use the generic that is wanted (in this case the currently namedDocTypegeneric (mongoose 7.3.1)Notes:
import "mongoose"has to be done where thedeclare module "mongoose"is happening, otherwise the wholemongoosemodule / namespace is overwritten instead of augmentedAll declarations of 'Document' must have identical type parameters.ts(2428)