-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Property 'updateDescription' does not exist on type 'Document<unknown, any #12942
Comments
Am able to reproduce |
nasty but works: type T1=any;
type AccountMethods = { checkPassword: (candidatePassword: string)=>Promise<boolean>; };
const AccountSchema = new Schema<T1,Model<T1, any, AccountMethods, any>,AccountMethods>({}); |
Does this issue just show up in VS Code highlighting, or is there an actual failure when you run |
@vkarpov15 failure when running tsc import * as mongoose from "mongoose";
// Define schema.
const dinosaurSchema = new mongoose.Schema({
name: { type: String, unique: true },
description: String,
createdAt: { type: Date, default: Date.now },
updatedAt: { type: Date, default: Date.now },
});
// Validations
dinosaurSchema.path("name").required(true, "Dinosaur name cannot be blank.");
dinosaurSchema.path("description").required(
true,
"Dinosaur description cannot be blank.",
);
// Methods.
dinosaurSchema.methods = {
// Update description.
updateDescription: async function (description: string) {
this.description = description;
return await this.save();
},
};
const Dinosaur = mongoose.model('Dinosaur', dinosaurSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
// Check to see connection status.
console.log(mongoose.connection.readyState);
// Create a new Dinosaur.
const deno = new Dinosaur({
name: "Deno",
description: "The fastest dinosaur ever lived.",
});
// // Insert deno.
await deno.save();
// Find Deno by name.
const denoFromMongoDb = await Dinosaur.findOne({ name: "Deno" });
console.log(
`Finding Deno in MongoDB -- \n ${denoFromMongoDb?.name}: ${denoFromMongoDb?.description}`,
);
// Update description for Deno and save it.
await denoFromMongoDb?.updateDescription(
"The fastest and most secure dinosaur ever lived.",
);
}
run(); |
If you don't want to mess with generics, the preferred approach is to define your methods directly in your call to the Mongoose // Define schema.
const dinosaurSchema = new mongoose.Schema({
name: { type: String, unique: true },
description: String,
createdAt: { type: Date, default: Date.now },
updatedAt: { type: Date, default: Date.now },
}, {
methods: {
updateDescription: async function (description: string) {
this.description = description;
return await this.save();
}
}
}); If you can't use that approach, you can pass generics to your interface DinosaurType {
name?: string;
description?: string;
createdAt: Date;
updatedAt: Date;
}
// Define schema.
const dinosaurSchema = new mongoose.Schema<DinosaurType, mongoose.Model<DinosaurType>, { updateDescription: Function }>({
name: { type: String, unique: true },
description: String,
createdAt: { type: Date, default: Date.now },
updatedAt: { type: Date, default: Date.now },
}); We'll add some notes to the Mongoose docs about this. |
…cs, add info on using methods with generics Fix #12942
docs(typescript): highlight auto type inference for methods and statics, add info on using methods with generics
Prerequisites
Mongoose version
6.8
Node.js version
18.13.0
MongoDB version
6.0.4
Operating system
Windows
Operating system version (i.e. 20.04, 11.3, 10)
10.0.22000
Issue
The last method above updateDescription code hints that the type is wrong:
Property 'updateDescription' does not exist on type 'Document<unknown, any, { createdAt: Date; updatedAt: Date; description?: string | undefined; name?: string | undefined; }> & { createdAt: Date; updatedAt: Date; description?: string | undefined; name?: string | undefined; } & { ...; }'.deno-ts(2339)
The text was updated successfully, but these errors were encountered: