version 5.11.0 mongoose
Reporting a bug
cannot pass two parameters to new Schema ();
error TS2554: Expected 0-1 arguments, but got 2.
20 const ModelSchema = new Schema(modelSettings.fields,{
~
21 ...defaultSchemaOptions,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 ...modelSettings.schemaOptions,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 });
What is the expected behavior?
Schema should accept two parameters.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
version 5.11.0 mongoose
version 12.18.3 node
the fault lies in mongoose/index.d.ts
``` class Schema {
/**
* Create a new schema
*/
constructor(definition?: SchemaDefinition);
the @types/mongoose/index.d.ts has the correct constructor definition.
class Schema
/**
* Schema constructor.
* When nesting schemas, (children in the example above), always declare
* the child schema first before passing it into its parent.
* @event init Emitted after the schema is compiled into a Model.
*/
constructor(definition?: SchemaDefinition, options?:SchemaOptions );
```
note the "options?:SchemaOptions".
my code is picking up the mongoose/index.d.ts, not @types but that's another battle.
thanks
i had this problem and i solved it by fixing versions on my package.json:
"devDependencies": {
"@types/mongoose": "5.10.1",
....
"dependencies": {
"mongoose": "5.10.1",
.....
i have to use old 5.10 since 5.11 is ruining my work.
i had this problem and i solved it by fixing versions on my package.json:
"devDependencies": {
"@types/mongoose": "5.10.1",
...."dependencies": {
"mongoose": "5.10.1",
.....i have to use old 5.10 since 5.11 is ruining my work.
@NaturesProphet mind sharing tsconfig, we still can't build. need to release to google cloud. it's a disaster!
thanks in advance.
Mongoose 5.11.0 released it's own official types file (#8108). Are there any docs on how to migrate from using @types/mongoose to official types?
i had this problem and i solved it by fixing versions on my package.json:
"devDependencies": {
"@types/mongoose": "5.10.1",
....
"dependencies": {
"mongoose": "5.10.1",
.....
i have to use old 5.10 since 5.11 is ruining my work.@NaturesProphet mind sharing tsconfig, we still can't build. need to release to google cloud. it's a disaster!
thanks in advance.
working tsconfig
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true
},
"exclude": [
"./node_modules/*"
]
}
you may need to change tsconfig.build.json aswell
{"compilerOptions": {"skipLibCheck": true},
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}
disaster here also :(
I have this problem too.
I managed to "fix" it by changing back to version 5.10.19 and also by doing
export const schemaName: Schema = new (Schema as any)(
{
started: { type: Date, required: false },
ended: { type: Date, required: false }
},
{ _id: false }
);
but they are not solutions...
also if I use a package that was built with the old version in an application that uses the new one I get this error
Type 'Schema' is not generic.
export declare const SchemaName: Schema<any>;
I think that it is caused by the same main error of the schema contracture because if I do the (schema as any) in the package and use it the error in the application does not pop up.
Hi,
I just tried to upgrade to 5.11 also and have 244 errors. I cannot build the project and need to downgrade.
Is there any documentation on how to migrate from @types/mongoose to the default type definition?
Is there any example usage. I looked in the example folder and could see anything?
Regards,
Tarek
Same here tons of errors after update to 5.11.
mostly this on different lines
node_modules/@types/mongoose/index.d.ts(822,24): error TS2314: Generic type 'Query<ResultType, DocType, T>' requires 3 type argument(s).
Same here, any chance to get the @types/mongoose packages into the core package? I think a lot of people are relying on it, and creating new - incompatiblity - types creates a lot of (unnecessary?) pain.
little excerpt:
../../types/mongoose.d.ts(90,1): error TS6200: Definitions of the following identifiers conflict with those in another file: CastError, Mongoose, Connection, Collection, Document, Schema, VirtualType, Array, Buffer, Decimal128, DocumentArray, Map, ObjectId, Subdocument, FilterQuery, UpdateQuery, DocumentDefinition, QueryCursor, Aggregate, SchemaType, NativeError, Error
../../types/mongoose.d.ts(797,22): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(987,17): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(987,30): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(1010,46): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(1022,17): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(1022,30): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(1048,46): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(1612,74): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(1615,62): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(1620,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(1623,65): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(1628,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(1956,8): error TS2415: Class 'Query<T, ResultType, DocType>' incorrectly extends base class 'DocumentQuery<T, any, {}>'.
Types of property 'all' are incompatible.
Type '{ (val: any[]): this; (path: string, val: any[]): this; }' is not assignable to type '{ (val: number): this; (path: string, val: number): this; }'.
Types of parameters 'val' and 'val' are incompatible.
Type 'number' is not assignable to type 'any[]'.
../../types/mongoose.d.ts(1956,8): error TS2428: All declarations of 'Query' must have identical type parameters.
../../types/mongoose.d.ts(2037,56): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2041,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2062,65): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2066,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2080,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2084,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2093,56): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2094,71): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2097,20): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2099,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2102,30): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2104,61): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2169,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2207,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2223,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2295,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2339,23): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2457,42): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2459,37): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2461,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2541,47): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2554,64): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2558,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2563,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(2569,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3316,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3330,65): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3334,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3348,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3352,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3401,71): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3406,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3496,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3522,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3557,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3563,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3616,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3644,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3671,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3687,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3791,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3796,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3802,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3807,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3818,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3828,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3834,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3839,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3845,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3850,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3856,6): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../types/mongoose.d.ts(3859,35): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1,1): error TS6200: Definitions of the following identifiers conflict with those in another file: CastError, Mongoose, Connection, Collection, Document, Schema, VirtualType, Array, Buffer, Decimal128, DocumentArray, Map, ObjectId, Subdocument, FilterQuery, UpdateQuery, DocumentDefinition, QueryCursor, Aggregate, SchemaType, NativeError, Error
../../node_modules/mongoose/index.d.ts(45,14): error TS2403: Subsequent variable declarations must have the same type. Variable 'SchemaTypes' must be of type 'typeof Types', but here has type 'typeof Types'.
../../node_modules/mongoose/index.d.ts(346,37): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(350,40): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(459,37): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(462,129): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(484,123): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(487,126): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(529,58): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(530,82): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(533,67): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(534,91): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(572,151): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(632,42): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(644,101): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(647,99): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(657,57): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(658,81): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(659,137): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(662,140): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(665,140): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(668,165): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(671,135): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(674,135): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(677,173): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(680,160): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(682,125): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(690,68): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(693,151): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(696,173): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(699,135): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(702,139): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(705,138): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(708,37): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(908,20): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(908,38): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(913,20): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(913,38): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(919,19): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(919,37): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(946,5): error TS2374: Duplicate string index signature.
../../node_modules/mongoose/index.d.ts(1320,13): error TS2428: All declarations of 'Query' must have identical type parameters.
../../node_modules/mongoose/index.d.ts(1320,13): error TS2430: Interface 'Query<T, ResultType, DocType>' incorrectly extends interface 'DocumentQuery<T, any, {}>'.
../../node_modules/mongoose/index.d.ts(1324,42): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1360,58): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1361,90): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1364,67): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1365,99): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1378,123): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1385,122): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1388,107): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1405,99): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1423,57): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1424,87): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1425,153): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1428,157): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1431,147): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1434,147): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1437,178): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1440,146): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1443,177): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1492,32): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1509,56): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1526,19): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1589,123): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1596,163): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1662,85): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1665,157): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1673,161): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
../../node_modules/mongoose/index.d.ts(1679,160): error TS2314: Generic type 'Query<T, ResultType, DocType>' requires 3 type argument(s).
src/mongoose/index.ts(168,4): error TS2322: Type 'Connection | undefined' is not assignable to type 'Connection'.
I'm experiencing this one too. Have fixed my mongoose version at 15.10.3 for now while I watch this thread :)
It probably has something to do with mongoose migrating the types, for some reason instead of taking the schema definition from @types/mongoose it goes directly to the mongoose index.d.ts which why the error is caused, I'm pretty sure because I'm running microservices with one service on a newer realease and one on an older one, both have almost similar schema definition, but one of them fails(newer) and other one(older) works perfectly
Changing "mongoose": "^5.x.xx" to "mongoose": "~5.x.xx" in package.json will solve these problems for now I guess.
We're working on fixes for any issues that pop up - in theory you shouldn't have any issues migrating from @types/mongoose to the new bindings. If you find any, please open a new issue with code samples, #9608 is a good example.
@simllll it looks like you're experiencing a problem because you're importing both @types/mongoose and Mongoose v5.11's type bindings. Can you try removing @types/mongoose and see if you still get compiler errors?
@vkarpov15 Yeah the thing is we actually forked this to our own needs, we have "custom"/generic object ids in our code.. e.g. ObjectId
The types we use, can handle this kind of object ids and also check if we e.g. have a document that has defined:
_house: TypeObjectId
that a mongoose query can only pass in a "house"-object id and not a tree object id.
Or at least it has to be an "id" and not just a string, this is the main use case to be honest. But it also helps in a lot of other places in our code base to distinguish between varius object id "types".
some small excerpt of this:
type MongooseQuery<T> = {
[P in keyof T]?: [Extract<T[P], TypeObjectId<any> | TypeObjectId<any>[]>] extends [never] // objects can also have "typeobjectids" as properties, also we allow value of "null" for all fields
? T[P] extends {}
? mongodb.Condition<MongooseQuery<T[P] | null>>
: mongodb.Condition<T[P] | null> // otherwise we ONLY allow "correctly casted" mongodb object ids
: mongodb.Condition<mongodb.ObjectId | null>;
};
type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N;
type IsAny<T> = IfAny<T, true, never>;
type MongooseUpdate<T> = {
[P in keyof T /* as Exclude<T, Function>*/]: [Extract<T[P], TypeObjectId<any>>] extends [never]
? Extract<T[P], string | number | boolean | Date> extends [never] // these types are safe to put back immeideatly
? Exclude<T[P], undefined> extends Array<any>
? MongooseUpdate<Exclude<T[P], undefined>>
: Omit<MongooseUpdate<Exclude<T[P], undefined>>, '_id'> & { _id?: mongodb.ObjectId } // if it's any kind of object though, check the properties of it too for "typeobjectid"
: T[P]
: IsAny<T[P]> extends never // extract typeobject id matches for "any", therefore check this here again
? mongodb.ObjectId
: T[P];
};
example model def
export interface IDBGarden extends Document {
_house: TypeObjectId<IHouse>;
_tree: TypeObjectId<ITree>;
}
and TypeObjectId is just defined as:
export type TypeObjectId<T> = string & { type: T };
and ObjectId itself is a function that checks if the id is valid, and returns a casted string to TypeObjectId.
export const ObjectId = <T = never>(input: T extends never ? never : unknown): TypeObjectId<T> => {
// ... check if input is a valid object id...
return input as TypeObjectId<T>;
}
I attached our definition file, in case it helps to understand or if you are willing/interested to take parts of this approach to your definitions ;-) It would be great to see this feature in the "core". Willing to help :)
Anyway before this comes to much out of scope: The main issue is, I cannot upgrade mongoose now, because I'm unable to use my type "improved" defs after that.
@vkarpov15 Has this already been released ?
I removed @types/mongoose and I'm still experiencing the problem.
@snava10 the fix for OP's issue hasn't been released yet, but will be released within the next few hours.
@simllll this is an interesting problem. Do you know if there's a way to make TypeScript use your custom bindings rather than Mongoose's for now? Or make it so that your bindings work in parallel with Mongoose's? I would love to discuss incorporating your changes, but first I want to make sure you're unblocked from upgrading.
i had this problem and i solved it by fixing versions on my package.json:
"devDependencies": {
"@types/mongoose": "5.10.1",
....
"dependencies": {
"mongoose": "5.10.1",
.....
i have to use old 5.10 since 5.11 is ruining my work.@NaturesProphet mind sharing tsconfig, we still can't build. need to release to google cloud. it's a disaster!
thanks in advance.
Yes bro it's a TOTAL DISASTER! it happens when theres no good tests when someone commit on master.
this is my current tsconfig:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"resolveJsonModule": true
}
}
I wont use mongoose anymore. this is a total IRRESPONSIBILITY! A DISASTER!
I wont use mongoose anymore. this is a total IRRESPONSIBILITY! A DISASTER!
Relax ;-) This is open source software and @vkarpov15 and others are doing a great job and put in a lot of time to maintain this library! It "only" breaks types for now, and there is no real sem-ver for types either, these problems occur on @types almost every day. But it's not that you are left alone, everyone here tries to find a solution and fixes are already commited :) So I recommend just downgrade to an older version in the meantime and stick to it (there are some comments alreyd how you can do this), till you find time to upgrade to the new one.
And regarding tests: Mongoose has a great test suite ;) (well not for types (yet), but for the library itself)! Just be sure you use a package-lock or yarn lock file and don't do "implicit" updates.
@simllll this is an interesting problem. Do you know if there's a way to make TypeScript use your custom bindings rather than Mongoose's for now? Or make it so that your bindings work in parallel with Mongoose's? I would love to discuss incorporating your changes, but first I want to make sure you're unblocked from upgrading.
The only way I know of is doing declaration merging, but therefore it would need an interface. I think it will be doable by e.g. defintining the "ObjectId" in an interface, which can be overwritten from outside,.. I'm happy to look into this. But guessing on my current knowledge it's not a "quick fix"...
If someone else has an idea how to "ignore" or "overwrite" type definitions, please let me know (Except from listing all types in tsconfig which I want to use, this is unfortunately not an option, as we have probaly more than 100 type defs out there ;)).
@simllll I opened a separate issue to discuss this: https://github.com/Automattic/mongoose/issues/9614. OP's issue is fixed by https://github.com/Automattic/mongoose/commit/13140c019edcfaaec3df3a4a1132671675f2178d.
If you've been affected by this issue, I'm very sorry for any inconvenience. To work around it:
@types/mongoose
from package.json and remove it from your file system: rm -rf ./node_modules/@types/mongoose
If you still have issues compiling, please open a new issue and follow the issue template. If you're using a fork of @types/mongoose, follow #9614 for updates.
Most helpful comment
Mongoose 5.11.0 released it's own official types file (#8108). Are there any docs on how to migrate from using @types/mongoose to official types?