Skip to content

Commit bc13f15

Browse files
authored
Merge pull request #14734 from Automattic/vkarpov15/gh-14697-2
types: avoid automatically inferring lean result type when assigning to explicitly typed variable
2 parents d30a3b9 + d375603 commit bc13f15

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

test/types/document.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
DefaultSchemaOptions
1111
} from 'mongoose';
1212
import { DeleteResult } from 'mongodb';
13-
import { expectAssignable, expectError, expectType } from 'tsd';
13+
import { expectAssignable, expectError, expectNotAssignable, expectType } from 'tsd';
1414
import { autoTypedModel } from './models.test';
1515
import { autoTypedModelConnection } from './connection.test';
1616
import { AutoTypedSchemaType } from './schema.test';
@@ -42,7 +42,8 @@ void async function main() {
4242

4343
expectType<DeleteResult>(await doc.deleteOne());
4444
expectType<TestDocument | null>(await doc.deleteOne().findOne());
45-
expectType<{ _id: Types.ObjectId, name?: string } | null>(await doc.deleteOne().findOne().lean());
45+
expectAssignable<{ _id: Types.ObjectId, name?: string } | null>(await doc.deleteOne().findOne().lean());
46+
expectNotAssignable<TestDocument | null>(await doc.deleteOne().findOne().lean());
4647
}();
4748

4849

test/types/plugin.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ interface TestStaticMethods {
4949
findSomething(this: TestModel): Promise<TestDocument>;
5050
}
5151
type TestDocument = HydratedDocument<Test, TestVirtuals & TestInstanceMethods>;
52-
type TestQuery = Query<any, TestDocument, TestQueryHelpers> & TestQueryHelpers;
52+
type TestQuery = Query<any, TestDocument, TestQueryHelpers, Test> & TestQueryHelpers;
5353
interface TestQueryHelpers {
5454
whereSomething(this: TestQuery): this
5555
}

test/types/queries.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ const schema: Schema<ITest, Model<ITest, QueryHelpers>, {}, QueryHelpers> = new
3838
endDate: Date
3939
});
4040

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

45-
schema.query.byName = function(name: string): QueryWithHelpers<any, ITest, QueryHelpers> {
45+
schema.query.byName = function(name: string): QueryWithHelpers<ITest[], ITest, QueryHelpers> {
4646
expectError(this.notAQueryHelper());
4747
return this._byName(name);
4848
};

types/query.d.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ declare module 'mongoose' {
224224
: MergeType<ResultType, Paths>
225225
: MergeType<ResultType, Paths>;
226226

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

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

550550
/** Sets the lean option. */
551-
lean<
552-
LeanResultType = GetLeanResultType<RawDocType, ResultType, QueryOp>
553-
>(
551+
lean(
552+
val?: boolean | any
553+
): QueryWithHelpers<
554+
ResultType extends null
555+
? GetLeanResultType<RawDocType, ResultType, QueryOp> | null
556+
: GetLeanResultType<RawDocType, ResultType, QueryOp>,
557+
DocType,
558+
THelpers,
559+
RawDocType,
560+
QueryOp,
561+
TInstanceMethods
562+
>;
563+
lean<LeanResultType>(
554564
val?: boolean | any
555565
): QueryWithHelpers<
556566
ResultType extends null

0 commit comments

Comments
 (0)