Skip to content

Commit

Permalink
fix(types): avoid inferring doc type from param to create()
Browse files Browse the repository at this point in the history
Fix #12001
Re: #11960
Re: #11563
  • Loading branch information
vkarpov15 committed Jul 1, 2022
1 parent 39f651c commit 9ba3f12
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
8 changes: 4 additions & 4 deletions test/types/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const schema: Schema = new Schema({ name: { type: 'String' } });

interface ITest {
_id?: Types.ObjectId;
name?: string;
name: string;
}

const Test = model<ITest>('Test', schema);
Expand Down Expand Up @@ -105,7 +105,7 @@ Test.insertMany({ _id: '000000000000000000000000', name: 'test' }, (err, docs) =

Test.insertMany({ _id: new Types.ObjectId('000000000000000000000000') }, (err, docs) => {
expectType<Types.ObjectId>(docs[0]._id);
expectType<string | undefined>(docs[0].name);
expectType<string>(docs[0].name);
expectType<boolean>(docs[0].isNew);
});

Expand Down Expand Up @@ -153,13 +153,13 @@ Test.insertMany([{ name: 'test' }], { lean: true }).then(docs => {

Test.insertMany([{ name: 'test' }], { lean: false }).then(docs => {
expectType<Types.ObjectId>(docs[0]._id);
expectType<string | undefined>(docs[0].name);
expectType<string>(docs[0].name);
expectType<boolean>(docs[0].isNew);
});

Test.insertMany([{ name: 'test' }], { }).then(docs => {
expectType<Types.ObjectId>(docs[0]._id);
expectType<string | undefined>(docs[0].name);
expectType<string>(docs[0].name);
expectType<boolean>(docs[0].isNew);
});

Expand Down
1 change: 0 additions & 1 deletion test/types/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ export function autoTypedModel() {
// Model-functions-test
// Create should works with arbitrary objects.
const randomObject = await AutoTypedModel.create({ unExistKey: 'unExistKey', description: 'st' });
expectType<string>(randomObject.unExistKey);
expectType<AutoTypedSchemaType['schema']['userName']>(randomObject.userName);

const testDoc1 = await AutoTypedModel.create({ userName: 'M0_0a' });
Expand Down
10 changes: 5 additions & 5 deletions types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ declare module 'mongoose' {
countDocuments(callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;

/** Creates a new document or documents */
create<DocContents = T>(docs: Array<T | DocContents>, options?: SaveOptions): Promise<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>[]>;
create<DocContents = T>(docs: Array<T | DocContents>, callback: Callback<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>[]>): void;
create<DocContents = T>(doc: DocContents | T): Promise<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>>;
create<DocContents = T>(...docs: Array<T | DocContents>): Promise<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>[]>;
create<DocContents = T>(doc: T | DocContents, callback: Callback<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>>): void;
create<DocContents = AnyKeys<T>>(docs: Array<T | DocContents>, options?: SaveOptions): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>[]>;
create<DocContents = AnyKeys<T>>(docs: Array<T | DocContents>, callback: Callback<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>[]>): void;
create<DocContents = AnyKeys<T>>(doc: DocContents | T): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>;
create<DocContents = AnyKeys<T>>(...docs: Array<T | DocContents>): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>[]>;
create<DocContents = AnyKeys<T>>(doc: T | DocContents, callback: Callback<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>): void;

/**
* Create the collection for this model. By default, if no indexes are specified,
Expand Down

0 comments on commit 9ba3f12

Please sign in to comment.