Mongoose is causing 100+ type errors that break the build?

Viewed 140

I am trying to get a server build running for a small API. However, I've been hitting a painful problem. Mongoose fails to compile under TypeScript, due to it's internal build of MongoDB. This problem is similar but not an exact repeat of others - I've found a few of them, but their solutions have not worked.

This is the kind of output I've gotten, error wise:

> servapi@1.0.0 clean
> node node_modules/rimraf/bin lib

node_modules/mongodb/mongodb.d.ts:3571:117 - error TS1005: '?' expected.

3571 export declare type Join<T extends unknown[], D extends string> = T extends [] ? '' : T extends [string | number] ? `${T[0]}` : T extends [string | number, ...infer R] ? `${T[0]}${D}${Join<R, D>}` : string;
                                                                                                                        

node_modules/mongodb/mongodb.d.ts:3571:127 - error TS1005: ';' expected.

3571 export declare type Join<T extends unknown[], D extends string> = T extends [] ? '' : T extends [string | number] ? `${T[0]}` : T extends [string | number, ...infer R] ? `${T[0]}${D}${Join<R, D>}` : string;
                                                                                                                               

node_modules/mongodb/mongodb.d.ts:3571:131 - error TS1005: ';' expected.

3571 export declare type Join<T extends unknown[], D extends string> = T extends [] ? '' : T extends [string | number] ? `${T[0]}` : T extends [string | number, ...infer R] ? `${T[0]}${D}${Join<R, D>}` : string;
                                                                                                                                  

node_modules/mongodb/mongodb.d.ts:3571:166 - error TS1005: ',' expected.

3571 export declare type Join<T extends unknown[], D extends string> = T extends [] ? '' : T extends [string | number] ? `${T[0]}` : T extends [string | number, ...infer R] ? `${T[0]}${D}${Join<R, D>}` : string;
                                                                                                                                                                      
node_modules/mongodb/mongodb.d.ts:3571:195 - error TS1005: '(' expected.

3571 export declare type Join<T extends unknown[], D extends string> = T extends [] ? '' : T extends [string | number] ? `${T[0]}` : T extends [string | number, ...infer R] ? `${T[0]}${D}${Join<R, D>}` : string;
                                                                                                                                                                                                   

node_modules/mongodb/mongodb.d.ts:4021:7 - error TS1005: ',' expected.

4021  * 
~~~~~~~~~~~

Found 114 errors.

Basically..everything in mongodb fails. I get multiple such type errors. External types are no longer maintained, so those cannot be installed to help.

Updating Mongoose and MongoDB to the latest version at least reduced the errors from 200+.

This is my current tsconfig and package.json:

tsconfig:

{
  "compilerOptions": {
    "outDir": "lib",
    "target": "es6",
    "module": "commonjs",
    "strict": false,
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "strictNullChecks": false,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true
  },
  "include": [
    "src",
    "src/data"
  ],
  "exclude": [
    "node_modules"
  ]
}

package.json:

    {
      "private": true,
      "name": "servapi",
      "version": "1.0.0",
      "description": "API Server",
      "main": "lib/index.js",
      "directories": {
        "data": "src/data"
      },
      "scripts": {
        "start": "ts-node-dev --respawn --transpile-only src/index.ts",
        "build": "npm run clean && tsc && node node_modules/copyfiles/copyfiles package.json ./lib && node node_modules/copyfiles/copyfiles ./lib",
        "clean": "node node_modules/rimraf/bin lib"
      },
      "author": "",
      "license": "UNLICENSED",
      "devDependencies": {
        "@types/cors": "^2.8.6",
        "@types/express": "^4.17.1",
        "@types/mocha": "^8.2.3",
        "copyfiles": "^2.4.1",
        "mocha": "^9.0.2",
        "rimraf": "^2.7.1",
        "ts-node": "^8.1.0",
        "ts-node-dev": "^1.0.0-pre.63",
        "typescript": "^3.7.0"
      },
      "dependencies": {
        "@types/multer": "^1.4.7",
        "axios": "^0.22.0",
        "cors": "^2.8.5",
        "escape-goat": "^4.0.0",
        "express": "^4.16.4",
        "mongoose": "^6.3.8",
        "multer": "^1.4.5-lts.1"
      }
    }

It does appear to at least partially build, but halts when it hits the index.js. Adding "skipLibCheck" doesn't seem to work - my guess would be because MongoDB is being called by Mongoose. How can I fix these type errors?

1 Answers

The answer actually turned out to be a TypeScript problem.

For whatever reason, earlier versions don't like the current typing in Mongo/ose, etc. If you come across this problem - upgrade to the latest TypeScript version if you can do so.

For me, that was by forcing TS to the latest version (mine was around 4.7.2) - Remove the ~ in your package.json, change the TypeScript version manually and do an NPM Update.

Related