Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): add HydratedDocumentFromSchema to make it easier to pull inferred hydrated doc type #12464

Merged
merged 1 commit into from Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.json
Expand Up @@ -25,6 +25,7 @@
"rules": {
"@typescript-eslint/triple-slash-reference": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-empty-function": "off",
"spaced-comment": [
"error",
"always",
Expand Down
26 changes: 26 additions & 0 deletions test/types/models.test.ts
Expand Up @@ -9,6 +9,7 @@ import {
UpdateQuery,
CallbackError,
HydratedDocument,
HydratedDocumentFromSchema,
LeanDocument,
Query,
UpdateWriteOpResult
Expand Down Expand Up @@ -494,3 +495,28 @@ async function gh12347() {
const replaceOneResult = await User.replaceOne({}, {});
expectType<UpdateWriteOpResult>(replaceOneResult);
}

async function gh12319() {
const projectSchema = new Schema(
{
name: {
type: String,
required: true
}
},
{
methods: {
async doSomething() {
}
}
}
);

const ProjectModel = model('Project', projectSchema);

type ProjectModelHydratedDoc = HydratedDocumentFromSchema<
typeof projectSchema
>;

expectType<ProjectModelHydratedDoc>(await ProjectModel.findOne().orFail());
}
6 changes: 6 additions & 0 deletions types/index.d.ts
Expand Up @@ -118,6 +118,12 @@ declare module 'mongoose' {

export type HydratedDocument<DocType, TMethodsAndOverrides = {}, TVirtuals = {}> = DocType extends Document ? Require_id<DocType> : (Document<unknown, any, DocType> & Require_id<DocType> & TVirtuals & TMethodsAndOverrides);

export type HydratedDocumentFromSchema<TSchema extends Schema> = HydratedDocument<
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
InferSchemaType<TSchema>,
ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>
>;

export interface TagSet {
[k: string]: string;
}
Expand Down