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


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

对于前端代码的编写,我比较推崇TypeScript (opens new window),并且既然用了TypeScript,我们就需要把任何变量的类型写全了,而不是偷懒使用any

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

'"devServer"' does not exist in type 'Configuration'
1

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
    ...
  }
  ...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

参考文档: