webpack-streams breaks @types/webpack



我有一个gulpfile,我正在用typescript构建。一切都在工作,直到我添加@types/webpack-stream,然后出于某种原因,我得到一个非常丑陋的错误:

node_modules/@types/webpack-stream/index.d.ts:23:24 - error TS2694: Namespace 'exports' has no exported member 'Compiler'.
23     callback?: webpack.Compiler.Handler,
~~~~~~~~
node_modules/@types/webpack/index.d.ts:32:3 - error TS2305: Module '"tapable"' has no exported member 'Tapable'.
32   Tapable,
~~~~~~~
node_modules/@types/webpack/index.d.ts:1081:23 - error TS2707: Generic type 'SyncWaterfallHook<T, AdditionalOptions>' requires between 1 and 2 type arguments.
1081             resolver: SyncWaterfallHook;
...
[lots of other types not working here]
...
Found 90 errors.

下面是我的package.json

{
"type": "module",
"engines": {
"node": "^16.12.0"
},
"scripts": {
"dev": "tsc --watch",
"gulp": "yarn gulp:build && yarn gulp:run",
"gulp:build": "tsc --project gulp",
"gulp:run": "gulp"
},
"dependencies": {
"highlight.js": "^11.3.1"
},
"devDependencies": {
"@babel/cli": "^7.15.7",
"@babel/core": "^7.15.8",
"@babel/plugin-transform-modules-amd": "^7.14.5",
"@babel/plugin-transform-modules-commonjs": "^7.15.4",
"@babel/plugin-transform-modules-systemjs": "^7.15.4",
"@babel/plugin-transform-modules-umd": "^7.14.5",
"@babel/preset-env": "^7.15.8",
"@types/gulp": "^4.0.9",
"@types/gulp-babel": "^6.1.30",
"@types/gulp-if": "^0.0.34",
"@types/gulp-rename": "^2.0.1",
"@types/gulp-sourcemaps": "^0.0.35",
"@types/gulp-terser": "^1.2.1",
"@types/node": "^16.11.5",
"@types/webpack-stream": "^3.2.12",
"gulp": "^4.0.2",
"gulp-babel": "^8.0.0",
"gulp-cli": "^2.3.0",
"gulp-if": "^3.0.0",
"gulp-rename": "^2.0.0",
"gulp-sourcemaps": "^3.0.0",
"gulp-terser": "^2.1.0",
"gulp-typescript": "^6.0.0-alpha.1",
"rambda": "^6.9.0",
"typescript": "^4.4.4",
"webpack": "^5.61.0",
"webpack-cli": "^4.9.1",
"webpack-stream": "^7.0.0"
}
}

在我添加这个包之前,我考虑不添加它,但是TS对我尖叫,因为:

Could not find a declaration file for module 'webpack-stream'. '/node_modules/webpack-stream/index.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/webpack-stream` if it exists or add a new declaration (.d.ts) file containing `declare module 'webpack-stream';`ts(7016)`

但是当我添加它时,它就是不想工作。我试着重新定义node_modules/@types/webpack-stream/index.d.ts:23:24的第一个错误,看看它是否是这样的一切:

callback?: Parameters<webpack.Compiler["run"]>[0] | Parameters<webpack.Compiler["watch"]>[1]

,修复第一个错误,但其余的保持不变。我不知道从这里去哪里,我可以在TS中重新实现webpack-streams

找到解决方案了!

首先,到@types/webpack-stream,在依赖项下我们得到@types/webpack@types/node

由于不同的原因,我已经安装了@types/node,所以:

  • 删除@types/webpack-stream
  • 添加@types/webpack
  • 再次添加了@types/webpack-stream(以确保pnp正在解析到我想要的@types版本)

在此之上,我有gulp/目录,我保存我的gulpfile.ts,我确保tsconfig.json有以下内容:

{
"compilerOptions": {
"skipLibCheck": true
}
}

相关内容

  • 没有找到相关文章

最新更新