Skip to content

Commit

Permalink
fix(index.d.ts): allow new Model<DocType>(obj) for stricter type ch…
Browse files Browse the repository at this point in the history
…ecks

Re: #11148
  • Loading branch information
vkarpov15 committed Jan 5, 2022
1 parent 0ad0d94 commit e9cf113
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ declare module 'mongoose' {

export const Model: Model<any>;
interface Model<T, TQueryHelpers = {}, TMethods = {}, TVirtuals = {}> extends NodeJS.EventEmitter, AcceptsDiscriminator {
new(doc?: AnyKeys<T> & AnyObject, fields?: any | null, options?: boolean | AnyObject): HydratedDocument<T, TMethods, TVirtuals>;
new<DocType = AnyKeys<T> & AnyObject>(doc?: DocType, fields?: any | null, options?: boolean | AnyObject): HydratedDocument<T, TMethods, TVirtuals>;

aggregate<R = any>(pipeline?: PipelineStage[], options?: mongodb.AggregateOptions, callback?: Callback<R[]>): Aggregate<Array<R>>;
aggregate<R = any>(pipeline: PipelineStage[], cb: Function): Aggregate<Array<R>>;
Expand Down
4 changes: 3 additions & 1 deletion test/typescript/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ describe('typescript syntax', function() {
it('models', function() {
const errors = runTest('models.ts');
printTSErrors(errors);
assert.equal(errors.length, 0);
assert.equal(errors.length, 1);
const messageText = errors[0].messageText.messageText;
assert.ok(/Argument of type .* not assignable to parameter of type .*foo: string;.*/.test(messageText), messageText);
});

it('methods', function() {
Expand Down
2 changes: 2 additions & 0 deletions test/typescript/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ function conventionalSyntax(): void {
const doc = new Test({ foo: '42' });
console.log(doc.foo);
doc.save();

const doc2 = new Test<{ foo: string }>({});
}

function rawDocSyntax(): void {
Expand Down

0 comments on commit e9cf113

Please sign in to comment.