Mer*_*lin 6 mongoose mongodb node.js typescript typescript-typings
我正在尝试为小型 API 运行服务器构建。然而,我遇到了一个痛苦的问题。Mongoose 无法在 TypeScript 下编译,因为它是 MongoDB 的内部构建。这个问题是类似的,但不是其他问题的精确重复 - 我找到了其中的一些,但他们的解决方案不起作用。
这是我得到的输出,有错误:
> 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.
Run Code Online (Sandbox Code Playgroud)
基本上..mongodb 中的一切都失败了。我收到多个此类类型错误。外部类型不再维护,因此无法安装这些类型来提供帮助。
将 Mongoose 和 MongoDB 更新到最新版本至少减少了 200+ 的错误。
这是我当前的 tsconfig 和 package.json:
ts配置:
{
"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"
]
}
Run Code Online (Sandbox Code Playgroud)
包.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"
}
}
Run Code Online (Sandbox Code Playgroud)
它似乎至少部分构建,但当它到达index.js 时停止。添加“skipLibCheck”似乎不起作用 - 我的猜测是因为 Mongoose 正在调用 MongoDB。如何修复这些类型错误?
答案实际上是 TypeScript 问题。
无论出于何种原因,早期版本不喜欢 Mongo/ose 等中的当前输入。如果您遇到此问题 - 如果可以的话,请升级到最新的 TypeScript 版本。
对我来说,这是通过强制 TS 到最新版本(我的是 4.7.2 左右) - 删除 package.json 中的 ~,手动更改 TypeScript 版本并执行 NPM 更新。
| 归档时间: |
|
| 查看次数: |
1790 次 |
| 最近记录: |