Skip to content

Commit

Permalink
fix(index.d.ts): make Document#id optional so types that use id c…
Browse files Browse the repository at this point in the history
…an use `Model<IMyType & Document>`

Fix #9684
  • Loading branch information
vkarpov15 committed Dec 10, 2020
1 parent a9b317a commit d318339
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Expand Up @@ -475,7 +475,7 @@ declare module "mongoose" {
getChanges(): UpdateQuery<this>;

/** The string version of this documents _id. */
id: string;
id?: string;

/** Signal that we desire an increment of this documents version. */
increment(): this;
Expand Down
38 changes: 27 additions & 11 deletions test/typescript/models.ts
@@ -1,19 +1,37 @@
import { Schema, Document, Model, connection } from 'mongoose';

interface ITest extends Document {
foo: string;
function conventionalSyntax(): void {
interface ITest extends Document {
foo: string;
}

const TestSchema = new Schema({
foo: { type: String, required: true },
});

const Test = connection.model<ITest>('Test', TestSchema);

const bar = (SomeModel: Model<ITest>) => console.log(SomeModel);

bar(Test);
}

const TestSchema = new Schema({
foo: { type: String, required: true },
});
function tAndDocSyntax(): void {
interface ITest {
id: number;
foo: string;
}

const TestSchema = new Schema({
foo: { type: String, required: true },
});

const Test = connection.model<ITest>('Test', TestSchema);
const Test = connection.model<ITest & Document>('Test', TestSchema);

const bar = (SomeModel: Model<ITest>) => // <<<< error here
console.log(SomeModel);
const aggregated: Promise<Document> = Test.aggregate([]).then(res => res[0]);

bar(Test);
const bar = (SomeModel: Model<ITest & Document>) => console.log(SomeModel);
}

const ExpiresSchema = new Schema({
ttl: {
Expand All @@ -22,8 +40,6 @@ const ExpiresSchema = new Schema({
},
});

const aggregated: Promise<Document> = Test.aggregate([]).then(res => res[0]);

interface IProject extends Document {
name: String;
}
Expand Down

0 comments on commit d318339

Please sign in to comment.