Definitelytyped: Installing @types/mongoose breaks the compiler

Created on 22 Feb 2019  路  6Comments  路  Source: DefinitelyTyped/DefinitelyTyped

If you know how to fix the issue, make a pull request instead.

  • [x] I tried using the @types/xxxx package and had problems.
  • [x] I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
  • [x] I have a question that is inappropriate for StackOverflow. (Please ask any appropriate questions there).
  • [x] [Mention](https://github.com/blog/821-mention-somebody-they-re-notified) the authors (see Definitions by: in index.d.ts) so they can respond.

    • @horiuchi

    • @lukasz-zak

    • @Alorel

    • @jendrikw

    • @ethanresnick

    • @vologab

    • @jussikinnula

    • @ondratra

    • @alfirin

    • @idandrd

    • @various89

    • @Fazendaaa

    • @danmana

    • @stablio

    • @emmanuelgautier

    • @frontendmonster

    • @mingchen

Dozen of authors 馃槃

In short @types/[email protected] keeps breaking my compilation.

OS: Windows 10
Node: v10.15.0
Typescript: v3.3.3

Error log:

node_modules/@types/connect-mongo/node_modules/@types/mongoose/index.d.ts:79:1 - error TS6200: Definitions of the following identifiers conflict with those in another file: NativeBuffer, NativeDate, NativeError, Mongoose, CastError, ConnectionBase, Connection, Error, VirtualType, Schema, MongooseDocument, Subdocument, Array, DocumentArray, Buffer, ObjectIdConstructor, Decimal128, Embedded, Query, DocumentQuery, mquery, Aggregate, SchemaType

79 declare module "mongoose" {
   ~~~~~~~

  node_modules/@types/mongoose/index.d.ts:55:1
    55 declare module "mongoose" {
       ~~~~~~~
    Conflicts are in this file.

node_modules/@types/connect-mongo/node_modules/@types/mongoose/index.d.ts:715:48 - error TS2314: Generic type 'HookAsyncCallback<T>' requires 1 type argument(s).

715     pre(method: string, parallel: boolean, fn: HookAsyncCallback, errorCb?: HookErrorCallback): this;
                                                   ~~~~~~~~~~~~~~~~~

node_modules/@types/connect-mongo/node_modules/@types/mongoose/index.d.ts:716:29 - error TS2314: Generic type 'HookSyncCallback<T>' requires 1 type argument(s).

716     pre(method: string, fn: HookSyncCallback, errorCb?: HookErrorCallback): this;
                                ~~~~~~~~~~~~~~~~

node_modules/@types/connect-mongo/node_modules/@types/mongoose/index.d.ts:778:13 - error TS2428: All declarations of 'HookSyncCallback' must have identical type parameters.

778   interface HookSyncCallback {
                ~~~~~~~~~~~~~~~~

node_modules/@types/connect-mongo/node_modules/@types/mongoose/index.d.ts:782:13 - error TS2428: All declarations of 'HookAsyncCallback' must have identical type parameters.

782   interface HookAsyncCallback {
                ~~~~~~~~~~~~~~~~~

node_modules/@types/mongoose/index.d.ts:55:1 - error TS6200: Definitions of the following identifiers conflict with those in another file: NativeBuffer, NativeDate, NativeError, Mongoose, CastError, ConnectionBase, Connection, Error, VirtualType, Schema, MongooseDocument, Subdocument, Array, DocumentArray, Buffer, ObjectIdConstructor, Decimal128, Embedded, Query, DocumentQuery, mquery, Aggregate, SchemaType

55 declare module "mongoose" {
   ~~~~~~~

  node_modules/@types/connect-mongo/node_modules/@types/mongoose/index.d.ts:79:1
    79 declare module "mongoose" {
       ~~~~~~~
    Conflicts are in this file.

node_modules/@types/mongoose/index.d.ts:84: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'.

84   export var SchemaTypes: typeof Schema.Types;
                ~~~~~~~~~~~

node_modules/@types/mongoose/index.d.ts:170:27 - error TS2702: 'Error' only refers to a type, but is being used as a namespace here.

170   export type CastError = Error.CastError;
                              ~~~~~

node_modules/@types/mongoose/index.d.ts:340:5 - error TS2717: Subsequent property declarations must have the same type.  Property 'sslValidate' must be of type 'object | undefined', but here has type 'boolean | undefined'.

340     sslValidate?: boolean;
        ~~~~~~~~~~~

node_modules/@types/mongoose/index.d.ts:346:5 - error TS2717: Subsequent property declarations must have the same type.  Property 'keepAlive' must be of type 'number | undefined', but here has type 'boolean | undefined'.

346     keepAlive?: boolean;
        ~~~~~~~~~

node_modules/@types/mongoose/index.d.ts:998:13 - error TS2428: All declarations of 'HookSyncCallback' must have identical type parameters.

998   interface HookSyncCallback<T> {
                ~~~~~~~~~~~~~~~~

node_modules/@types/mongoose/index.d.ts:1002:13 - error TS2428: All declarations of 'HookAsyncCallback' must have identical type parameters.

1002   interface HookAsyncCallback<T> {
                 ~~~~~~~~~~~~~~~~~

node_modules/@types/mongoose/index.d.ts:1048:5 - error TS2717: Subsequent property declarations must have the same type.  Property 'strict' must be of type 'boolean | undefined', but here has type 'boolean | "throw" | undefined'.

1048     strict?: boolean | 'throw';
         ~~~~~~

node_modules/@types/mongoose/index.d.ts:1050:5 - error TS2717: Subsequent property declarations must have the same type.  Property 'toJSON' must be of type 'any', but here has type 'DocumentToObjectOptions | undefined'.

1050     toJSON?: DocumentToObjectOptions;
         ~~~~~~

node_modules/@types/mongoose/index.d.ts:1052:5 - error TS2717: Subsequent property declarations must have the same type.  Property 'toObject' must be of type 'any', but here has type 'DocumentToObjectOptions | undefined'.

1052     toObject?: DocumentToObjectOptions;
         ~~~~~~~~

node_modules/@types/mongoose/index.d.ts:1074:5 - error TS2717: Subsequent property declarations must have the same type.  Property 'timestamps' must be of type 'any', but here has type 'boolean | SchemaTimestampsConfig | undefined'.

1074     timestamps?: boolean | SchemaTimestampsConfig;
         ~~~~~~~~~~

node_modules/@types/mongoose/index.d.ts:1086:5 - error TS2374: Duplicate string index signature.

1086     [path: string]: SchemaTypeOpts<any> | Schema | SchemaType;
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/@types/mongoose/index.d.ts:1166:5 - error TS2717: Subsequent property declarations must have the same type.  Property 'validate' must be of type 'any', but here has type 'RegExp | [RegExp, string] | ValidateFn<T> | [ValidateFn<T>, string] | ValidateOpts | AsyncValidateOpts | AsyncPromiseValidationFn<T> | AsyncPromiseValidationOpts | (ValidateOpts | ... 2 more ... | AsyncPromiseValidationOpts)[] | undefined'.

1166     validate?: RegExp | [RegExp, string] |
         ~~~~~~~~

node_modules/@types/mongoose/index.d.ts:1218:5 - error TS2374: Duplicate string index signature.

1218     [other: string]: any;
         ~~~~~~~~~~~~~~~~~~~~~

node_modules/@types/mongoose/index.d.ts:1246:7 - error TS2717: Subsequent property declarations must have the same type.  Property 'validator' must be of type 'Function | RegExp | undefined', but here has type 'RegExp | ValidateFn<any> | undefined'.

1246       validator?: RegExp | ValidateFn<any>;
           ~~~~~~~~~

node_modules/@types/mongoose/index.d.ts:1339:91 - error TS2702: 'Error' only refers to a type, but is being used as a namespace here.

1339     invalidate(path: string, errorMsg: string | NativeError, value?: any, kind?: string): Error.ValidationError | boolean;
                                                                                               ~~~~~

node_modules/@types/mongoose/index.d.ts:1439:56 - error TS2702: 'Error' only refers to a type, but is being used as a namespace here.

1439     validateSync(pathsToValidate?: string | string[]): Error.ValidationError;
                                                            ~~~~~

node_modules/@types/mongoose/index.d.ts:3277:5 - error TS2374: Duplicate string index signature.

3277     [k: string]: string;
         ~~~~~~~~~~~~~~~~~~~~

node_modules/@types/mongoose/index.d.ts:3386:5 - error TS2374: Duplicate string index signature.

3386     [other: string]: any;
         ~~~~~~~~~~~~~~~~~~~~~

Most helpful comment

I just resolved a similar issue by removing the version constraints off @types/mongoose in my package.json, so the package manager could figure out the correct compatible version after a fresh install.

All 6 comments

I'd love to look into this, but it's hard without a reproduceable example. Can you post your typescript code that uses mongoose and connect-mongo, the package.json and tsconfig.json?

I suspect typescript is compiling connect-mongo.

package.json

{
  "main": "main.ts",
  "scripts": {
    "build": "ts-node scripts/Build.ts",
    "lint": "tslint --project tsconfig.json",
    "mongodb": "mongod --dbpath \"./mongodb\"",
    "prebuild": "yarn pretty && yarn test",
    "precommit": "lint-staged && yarn test",
    "pretty": "prettier -l --write **/*.{js,ts,json} !**/build/** !**/coverage/**",
    "start": "nodemon",
    "test": "yarn lint && jest --coverage --passWithNoTests",
    "test:unit": "jest --coverage --passWithNoTests"
  },
  "lint-staged": {
    "*.ts": [
      "yarn pretty",
      "git add"
    ]
  },
  "dependencies": {
    "app-root-path": "^2.1.0",
    "bcrypt": "^3.0.4",
    "body-parser": "^1.18.3",
    "compression": "^1.7.3",
    "connect-mongo": "^2.0.3",
    "cookie-parser": "^1.4.4",
    "cookie-session": "^2.0.0-beta.3",
    "cors": "^2.8.5",
    "dotenv": "^6.2.0",
    "errorhandler": "^1.5.0",
    "express": "^4.16.4",
    "express-session": "^1.15.6",
    "express-winston": "^3.1.0",
    "fs-extra": "^7.0.1",
    "joi": "^14.3.1",
    "method-override": "^3.0.0",
    "methods": "^1.1.2",
    "moment": "^2.24.0",
    "mongoose": "^5.4.11",
    "morgan": "^1.9.1",
    "passport": "^0.4.0",
    "passport-facebook": "^3.0.0",
    "passport-google-oauth": "^1.0.0",
    "passport-instagram": "^1.0.0",
    "passport-local": "^1.0.0",
    "passport-twitter": "^1.0.4",
    "pm2": "^3.3.1",
    "socket.io": "^2.2.0",
    "winston": "^3.2.1"
  },
  "devDependencies": {
    "@babel/core": "^7.2.2",
    "@babel/node": "^7.2.2",
    "@babel/plugin-proposal-class-properties": "^7.3.3",
    "@babel/plugin-proposal-object-rest-spread": "^7.3.2",
    "@babel/preset-env": "^7.3.1",
    "@babel/preset-typescript": "^7.3.3",
    "@types/app-root-path": "^1.2.4",
    "@types/clean-webpack-plugin": "^0.1.2",
    "@types/compression": "^0.0.36",
    "@types/connect-mongo": "^0.0.40",
    "@types/cookie-parser": "^1.4.1",
    "@types/cookie-session": "^2.0.36",
    "@types/cors": "^2.8.4",
    "@types/dotenv": "^6.1.0",
    "@types/errorhandler": "^0.0.32",
    "@types/express": "^4.16.1",
    "@types/express-session": "^1.15.12",
    "@types/express-winston": "^3.0.1",
    "@types/fs-extra": "^5.0.5",
    "@types/jest": "^24.0.6",
    "@types/method-override": "^0.0.31",
    "@types/mongodb": "^3.1.19",
    "@types/mongoose": "^5.3.19",
    "@types/node": "^11.9.4",
    "@types/passport": "^1.0.0",
    "@types/passport-facebook": "^2.1.8",
    "@types/passport-google-oauth": "^1.0.39",
    "@types/webpack": "^4.4.24",
    "@types/webpack-env": "^1.13.7",
    "@types/webpack-node-externals": "^1.6.3",
    "awesome-typescript-loader": "^5.2.1",
    "babel-loader": "^8.0.5",
    "clean-webpack-plugin": "^1.0.1",
    "husky": "^1.3.1",
    "jest": "^24.1.0",
    "lint-staged": "^8.1.4",
    "nodemon": "^1.18.10",
    "prettier": "^1.16.4",
    "source-map-loader": "^0.2.4",
    "supertest": "^3.4.2",
    "ts-jest": "^24.0.0",
    "ts-node": "^8.0.2",
    "tslint": "^5.12.1",
    "tslint-config-airbnb": "^5.11.1",
    "typescript": "^3.3.3",
    "webpack": "^4.29.3",
    "webpack-cli": "^3.2.3",
    "webpack-node-externals": "^1.7.2"
  },
  "resolutions": {},
  "repository": "<url>",
  "private": true
}

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "target": "esnext",
    "module": "commonjs",
    "allowJs": false,
    "checkJs": false,
    "declaration": true,
    "sourceMap": true,
    "pretty": true,
    "outDir": "build",
    "rootDir": ".",
    "composite": true,
    "removeComments": true,
    "noEmit": true,
    "isolatedModules": false,
    "strict": true,
    "noUnusedLocals": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "node",
    "baseUrl": ".",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "resolveJsonModule": true
  },
  "files": ["package.json"], // to avoid error TS6307
  "include": ["app", "scripts"],
  "exclude": ["node_modules", "build"]
}

Also, this issue can probably classified as a duplicate of #26928.

I encountered something similar because I had one folder with the models and its own package.json+node_modules and another one with the code.

Both versions went out of sync, so the compiler was complaining.
Hope it helps

I just resolved a similar issue by removing the version constraints off @types/mongoose in my package.json, so the package manager could figure out the correct compatible version after a fresh install.

I just resolved a similar issue by removing the version constraints off @types/mongoose in my package.json, so the package manager could figure out the correct compatible version after a fresh install.

thanks it work

Was this page helpful?
0 / 5 - 0 ratings