如何修复[@types/webpack] 'devServer' does not exist in type 'Configuration'


如何修复[@types/webpack] 'devServer' does not exist in type 'Configuration'

对于前端代码的编写,我比较推崇TypeScript在新窗口打开,并且既然用了TypeScript,我们就需要把任何变量的类型写全了,而不是偷懒使用any

今天遇到一个问题,使用TypeScript语法来配置webpack时,提示是:

'"devServer"' does not exist in type 'Configuration'
Screenshot2022-03-14-23
Screenshot2022-03-14-23

尝试解决一下:

import { Configuration as WebpackConfiguration } from "webpack";
import { Configuration as WebpackDevServerConfiguration } from "webpack-dev-server";

interface Configuration extends WebpackConfiguration {
  devServer?: WebpackDevServerConfiguration;
}

export const configuration: Configuration = {
  ...
  devServer: {
    historyApiFallback: true,
    hot: true,
    port: 3000
    ...
  }
  ...
}

参考文档: