Skip to content

Commit

Permalink
Merge pull request #14734 from Automattic/vkarpov15/gh-14697-2
Browse files Browse the repository at this point in the history
types: avoid automatically inferring lean result type when assigning to explicitly typed variable
  • Loading branch information
vkarpov15 authored Jul 10, 2024
2 parents d30a3b9 + d375603 commit bc13f15
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
5 changes: 3 additions & 2 deletions test/types/document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
DefaultSchemaOptions
} from 'mongoose';
import { DeleteResult } from 'mongodb';
import { expectAssignable, expectError, expectType } from 'tsd';
import { expectAssignable, expectError, expectNotAssignable, expectType } from 'tsd';
import { autoTypedModel } from './models.test';
import { autoTypedModelConnection } from './connection.test';
import { AutoTypedSchemaType } from './schema.test';
Expand Down Expand Up @@ -42,7 +42,8 @@ void async function main() {

expectType<DeleteResult>(await doc.deleteOne());
expectType<TestDocument | null>(await doc.deleteOne().findOne());
expectType<{ _id: Types.ObjectId, name?: string } | null>(await doc.deleteOne().findOne().lean());
expectAssignable<{ _id: Types.ObjectId, name?: string } | null>(await doc.deleteOne().findOne().lean());
expectNotAssignable<TestDocument | null>(await doc.deleteOne().findOne().lean());
}();


Expand Down
2 changes: 1 addition & 1 deletion test/types/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ interface TestStaticMethods {
findSomething(this: TestModel): Promise<TestDocument>;
}
type TestDocument = HydratedDocument<Test, TestVirtuals & TestInstanceMethods>;
type TestQuery = Query<any, TestDocument, TestQueryHelpers> & TestQueryHelpers;
type TestQuery = Query<any, TestDocument, TestQueryHelpers, Test> & TestQueryHelpers;
interface TestQueryHelpers {
whereSomething(this: TestQuery): this
}
Expand Down
4 changes: 2 additions & 2 deletions test/types/queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ const schema: Schema<ITest, Model<ITest, QueryHelpers>, {}, QueryHelpers> = new
endDate: Date
});

schema.query._byName = function(name: string): QueryWithHelpers<any, ITest, QueryHelpers> {
schema.query._byName = function(name: string): QueryWithHelpers<ITest[], ITest, QueryHelpers> {
return this.find({ name });
};

schema.query.byName = function(name: string): QueryWithHelpers<any, ITest, QueryHelpers> {
schema.query.byName = function(name: string): QueryWithHelpers<ITest[], ITest, QueryHelpers> {
expectError(this.notAQueryHelper());
return this._byName(name);
};
Expand Down
18 changes: 14 additions & 4 deletions types/query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ declare module 'mongoose' {
: MergeType<ResultType, Paths>
: MergeType<ResultType, Paths>;

class Query<ResultType, DocType, THelpers = {}, RawDocType = DocType, QueryOp = 'find', TInstanceMethods = Record<string, never>> implements SessionOperation {
class Query<ResultType, DocType, THelpers = {}, RawDocType = unknown, QueryOp = 'find', TInstanceMethods = Record<string, never>> implements SessionOperation {
_mongooseOptions: MongooseQueryOptions<DocType>;

/**
Expand Down Expand Up @@ -548,9 +548,19 @@ declare module 'mongoose' {
j(val: boolean | null): this;

/** Sets the lean option. */
lean<
LeanResultType = GetLeanResultType<RawDocType, ResultType, QueryOp>
>(
lean(
val?: boolean | any
): QueryWithHelpers<
ResultType extends null
? GetLeanResultType<RawDocType, ResultType, QueryOp> | null
: GetLeanResultType<RawDocType, ResultType, QueryOp>,
DocType,
THelpers,
RawDocType,
QueryOp,
TInstanceMethods
>;
lean<LeanResultType>(
val?: boolean | any
): QueryWithHelpers<
ResultType extends null
Expand Down

0 comments on commit bc13f15

Please sign in to comment.