在开发中,遇到文件中引入webpack,但是webpack.ICompiler不一致的情况
//
import webpack from 'webpack';
import webpackHot from 'webpack-hot-middleware';
const hotMiddleware = (options: {
compiler: webpack.ICompiler;
opts?: webpackHot.MiddlewareOptions;
}) => {
webpackHot(options.compiler, options.opts);
// ******* 省略其它无用代码
};
export default hotMiddleware;
错误信息: 类型“import("/Users/lijc/project/nps/node_modules/@types/webpack/index").ICompiler”的参数不能赋给类型“import("/Users/liujc/project/nps/node_modules/@types/webpack-dev-middleware/node_modules/@types/webpack/index").ICompiler”的参数。
解决方法:
可以在tsconfig.json 里面配置 compilerOptions.paths的别名功能
{ "compilerOptions": { "paths": { // 在这里指定webpack使用的包 "webpack": ["node_modules/webpack", "node_modules/@types/webpack"] }, } }