Skip to content

Commit

Permalink
Refactor after rebasing
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammad0-0ahmad committed May 5, 2022
1 parent af60163 commit ad1827c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 17 deletions.
11 changes: 5 additions & 6 deletions test/types/lean.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async function gh11761() {
{
// make sure _id has been added to the type
const { _id, ...thing1 } = (await ThingModel.create({ name: 'thing1' })).toObject();
expectType<Types.ObjectId | undefined>(_id);
expectType<Types.ObjectId>(_id);

console.log({ _id, thing1 });
}
Expand Down Expand Up @@ -131,11 +131,10 @@ async function gh11118(): Promise<void> {
const docs = await UserModel.find().lean().exec();

for (const doc of docs) {
const _id: Types.ObjectId | undefined = doc._id;
const _id: Types.ObjectId = doc._id;
}
}


// Helpers to simplify checks
type IfAny<IFTYPE, THENTYPE> = 0 extends (1 & IFTYPE) ? THENTYPE : IFTYPE;
type IfUnknown<IFTYPE, THENTYPE> = unknown extends IFTYPE ? THENTYPE : IFTYPE;
Expand Down Expand Up @@ -271,7 +270,7 @@ async function _11761() {

const { _id, name, id, __v } = (await ThingModel.create({ name: 'Thing 1' })).toObject();
expectType<string>(name);
expectType<string | undefined>(id);
expectType<Types.ObjectId | undefined>(_id);
expectType<number | undefined>(__v);
expectType<string>(id);
expectType<Types.ObjectId>(_id);
expectType<number>(__v);
}
1 change: 1 addition & 0 deletions test/types/populate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,5 @@ async function _11532() {

if (!leanResult) return;
expectType<string>(leanResult.child.name);
expectType<number>(leanResult?.__v);
}
10 changes: 1 addition & 9 deletions types/document.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ declare module 'mongoose' {
__v?: number;
}

/**
* @summary Utility helps to prepare an accurate Document type.
* @description It merge the type of the Document and SchemaDefinition "raw document" & make sure to have the correct type of LeanDocumentBaseType.
* @param {T} T Instance of {@link Document} class.
* @param {RawDocType} RawDocType Schema's raw document type.
*/
type ResolveDocumentBaseType<T, RawDocType> = IfEquals<RawDocType, any> extends true ? T : PopulateAFromB<RawDocType, LeanDocumentBaseType>;

/**
* Generic types for Document:
* * T - the type of _id
Expand Down Expand Up @@ -243,7 +235,7 @@ declare module 'mongoose' {
toJSON<T = DocType>(options: ToObjectOptions & { flattenMaps: false }): LeanDocument<T>;

/** Converts this document into a plain-old JavaScript object ([POJO](https://masteringjs.io/tutorials/fundamentals/pojo)). */
toObject<T = DocType>(options?: ToObjectOptions): LeanDocument<ResolveDocumentBaseType<this, T>>;
toObject<T = DocType>(options?: ToObjectOptions): LeanDocument<IfEquals<T, any, this, T>>;

/** Clears the modified state on the specified path. */
unmarkModified(path: string): void;
Expand Down
4 changes: 2 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ declare module 'mongoose' {
j(val: boolean | null): this;

/** Sets the lean option. */
lean<LeanResultType = RawDocType extends Document ? LeanDocumentOrArray<ResultType> : LeanDocumentOrArrayWithRawType<ResultType, Require_id<RawDocType>>>(val?: boolean | any): QueryWithHelpers<LeanResultType, DocType, THelpers, RawDocType>;
lean<LeanResultType = RawDocType extends Document ? LeanDocumentOrArray<ResultType> : LeanDocumentOrArrayWithRawType<ResultType, RawDocType>>(val?: boolean | any): QueryWithHelpers<LeanResultType, DocType, THelpers, RawDocType>;

/** Specifies the maximum number of documents the query will return. */
limit(val: number): this;
Expand Down Expand Up @@ -2235,7 +2235,7 @@ declare module 'mongoose' {
/**
* @summary Picks LeanDocumentBaseType from {@link Document}.
*/
type InferLeanDocumentBaseType<T extends LeanDocumentBaseType> = Pick<T, keyof LeanDocumentBaseType>;
type InferLeanDocumentBaseType<T extends LeanDocumentBaseType> = Pick<PopulateAFromB<T, Required<LeanDocumentBaseType>>, keyof LeanDocumentBaseType>;

/**
* Documents returned from queries with the lean option enabled.
Expand Down

0 comments on commit ad1827c

Please sign in to comment.