Skip to content

Commit

Permalink
test: lint and test cases for #14545
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Apr 29, 2024
1 parent 16e6985 commit b6ffcfd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
35 changes: 35 additions & 0 deletions test/types/queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,3 +636,38 @@ async function gh14525() {
let doc2 = await ({} as ModelType).create({});
doc2 = await ({} as ModelType).findOne({}).populate('test').orFail().exec();
}

async function gh14545() {
type Test = {
_id: Types.ObjectId;

prop: string;
another: string;

createdAt: number;
updatedAt: number;
};

const schema = new Schema<Test>({
prop: { type: String },
another: { type: String },
createdAt: { type: Number },
updatedAt: { type: Number }
});

type TestDocument = HydratedDocument<Test>;
type TestModel = Model<Test, {}, {}, {}, TestDocument>;

type SlimTest = Pick<Test, '_id' | 'prop'>;
type SlimTestDocument = HydratedDocument<SlimTest>;

const M = model<Test, TestModel>('Test', schema);

const myDocs = await M.find({}).exec();
const myDoc = await M.findOne({}).exec();

const myProjections = await M.find({}).select<SlimTest>({ prop: 1 }).exec();
expectType<SlimTestDocument[]>(myProjections);
const myProjection = await M.findOne({}).select<SlimTest>({ prop: 1 }).exec();
expectType<SlimTestDocument | null>(myProjection);
}
15 changes: 7 additions & 8 deletions types/query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,14 +697,13 @@ declare module 'mongoose' {
RawDocTypeOverride,
{},
ResultType,
ResultType extends any[] ?
ResultType extends HydratedDocument<any>[] ?
HydratedDocument<RawDocTypeOverride>[] :
RawDocTypeOverride[] :
| (ResultType extends HydratedDocument<any>
? HydratedDocument<RawDocTypeOverride>
: RawDocTypeOverride)
| (null extends ResultType ? null : never)
ResultType extends any[]
? ResultType extends HydratedDocument<any>[]
? HydratedDocument<RawDocTypeOverride>[]
: RawDocTypeOverride[]
: (ResultType extends HydratedDocument<any>
? HydratedDocument<RawDocTypeOverride>
: RawDocTypeOverride) | (null extends ResultType ? null : never)
>,
DocType,
THelpers,
Expand Down

0 comments on commit b6ffcfd

Please sign in to comment.