Typings: Webpack.Compiler is not assignable to Compiler

Bug report

What is the current behavior?

Passing a new compiler instance from to a function which expects a Compiler instance causes a type error.

The following example will log the context twice without any errors however the typescript compiler will show two type errors:

const webpack = require("webpack");
/** @typedef {import('webpack').Compiler} WebpackCompiler */
/** @typedef {import('webpack/lib/Compiler')} Compiler */

const compiler = webpack(config);
console.log(compiler.context); // <- [ts] Property 'context' does not exist on type 'Compiler'.
someFunction(compiler); // <- [ts] Argument of type 'webpack.Compiler' is not assignable to parameter of type 'Compiler'.
/**
 * @param {Compiler} compiler
 */
function someFunction(compiler) {
   console.log(compiler.context);
}

type-error

What is the expected behavior?

webpack(config) should return an instance of Compiler or Webpack.Compiler should have a context property

Other relevant information:
webpack version: 4.14.0

Author: Fantashit

3 thoughts on “Typings: Webpack.Compiler is not assignable to Compiler

  1. Actually webpack returns either a Compiler or aMultiCompiler or a Watching or a MultiWatching. All mentioned classes are not typed yet.

  2. It looks like an inconsistency between webpack and @types/webpack. Eventually we will generate our own type definition from the source.

Comments are closed.