It turned out that the newest versions (at least 5+) of webpack support Typescript out of the box. So the algorithm is next:
- Create
tsconfig.jsonat the root level. Content:{ "compilerOptions": { "module": "CommonJS", "target": "ES5", "esModuleInterop": true, "checkJs": false, "strict": true, } }
- Rename all *
.jsfiles to*.ts - Type all of them:
- no more ugly
require, useimport. webpackpackage has typings out of the box.- You may find these types useful:
Configuration&RuleSetRule - To enable
devServerwrite this:interface Configuration extends WebpackConfiguration { devServer?: WebpackDevServerConfiguration; }
- You may find these types useful:
- Some of the popular plugins have types too.
- Some of them don’t have types at all:
- Create a
*.d.tsfile - Put there something like this:
declare module 'postcss-assets' { export default function postcssAssets(opts: { basePath: string; relative: boolean; }): unknown; }
- Create a
- no more ugly
- Make sure your
webpack.config.tsfile is placed at the root level. I mean exactly at the same spot wherenode_modulesis. Otherwise, you won’t be able to build it. NocompilerOptionshelped me. - Run webpack. It should work.