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
Typescript errors in 6.0.0 #10605
Comments
Confirmed all 3 issues with the below script. We're investigating and will have a fix or workaround later today 👍 import { connect, model, Schema } from 'mongoose';
run().catch(err => console.log(err));
async function run() {
await connect('mongodb://localhost:27017/test');
interface ITest {
arrayField?: string[];
object: {
value: number
};
}
const schema = new Schema<ITest>({
arrayField: [String],
object: {
type: {
value: {
type: Number,
}
}
}
});
schema.static('method', function() {
this.findOne();
});
const TestModel = model<ITest>('Test', schema);
await TestModel.findOne({ arrayField: 'some value' });
} |
Can confirm the above script compiles with d862a5a. As a workaround until we ship 6.0.1, you can do: For 1: await TestModel.findOne({ arrayField: { $all: ['some value'] } }); For 2: const schema = new Schema<ITest>({
arrayField: [String],
object: new Schema({
value: {
type: Number,
}
})
}); (3) goes away when you fix (2). |
Hey, I'm still having very similar issues as 2, except now only in places where i use an array of objects as a type, such as: roles: {
type: [{
id: {
type: String
}
}]
} Again, |
Small snippet to reproduce: interface ITestSchema extends mongoose.Document {
someObject: Array<{id: string}>
}
const testSchema = new mongoose.Schema<ITestSchema>({
someObject: {
type: [{
id: {
type: String,
},
}],
},
});
/*
TS2322: Type '{ type: { id: { type: StringConstructor; }; }[]; }' is not assignable to type 'typeof SchemaType | Function[] | Schema<any, any, any> | Schema<any, any, any>[] | readonly Schema<any, any, any>[] | SchemaTypeOptions<{ id: string; }[]> | ... 5 more ... | undefined'.
Types of property 'type' are incompatible.
Type '{ id: { type: StringConstructor; }; }[]' is not assignable to type 'Schema<{ id: string; }, Model<{ id: string; }, any, any>, {}>[] | readonly Schema<{ id: string; }, Model<{ id: string; }, any, any>, {}>[] | Schema<Document<any, any, any> & { ...; }, Model<...>, {}>[] | readonly Schema<...>[] | undefined'.
Type '{ id: { type: StringConstructor; }; }[]' is not assignable to type 'Schema<{ id: string; }, Model<{ id: string; }, any, any>, {}>[]'.
Type '{ id: { type: StringConstructor; }; }' is not assignable to type 'Schema<{ id: string; }, Model<{ id: string; }, any, any>, {}>'.
Object literal may only specify known properties, and 'id' does not exist in type 'Schema<{ id: string; }, Model<{ id: string; }, any, any>, {}>'.
*/ |
I am also facing typing issues after upgrading from mongoose 5 to 6. Mongoose: 6.0.4 import mongoose, { Schema, Document } from "mongoose";
interface ITestSchema extends Document {
anArray: string[];
}
async function main() {
const TestSchema: Schema = new Schema();
TestSchema.add({
anArray: [
{
type: String,
},
],
});
const TestModel = mongoose.model<ITestSchema>("Test", TestSchema);
await TestModel.findOne({ anArray: { $in: ["valueA", "valueB"] } }).exec();
// Type '{ $in: string[]; }' is not assignable to type 'Condition<string[]> | undefined'.
// Types of property '$in' are incompatible.
// Type 'string[]' is not assignable to type 'string[][]'.ts(2322)
} |
Do you want to request a feature or report a bug?
A bug
What is the current behavior?
Multiple typescript types seem to not be working well, or at least they are not documented in the migration guide.
For now, I used
//@ts-ignore
to suppress the issues, and I haven't run into any errors, leading me to believe this is just a bug in the typings.If the current behavior is a bug, please provide the steps to reproduce.
Find on array value:
Model with nested field:
Static methods
this
:My tsconfig:
What is the expected behavior?
These types don't show errors, or the changes are documented with an alternate way to do these things instead.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: 16.6.1
Typescript: 4.3.5
MongoDB: 5.0.2
Mongoose: 6.0.0
The text was updated successfully, but these errors were encountered: