fork-ts-checker-webpack-plugin icon indicating copy to clipboard operation
fork-ts-checker-webpack-plugin copied to clipboard

Type Checking Not Works with TypeScript 5.6+ and `build: true` Option

Open kku39 opened this issue 1 year ago • 5 comments

When I use [email protected]+ and set the fork-ts-checker-webpack-plugin's option typescript.build to true, the following problem occurs.

Current behavior

The webpack build completes with no errors even if there are type errors in source code such as:

const num: number = 'aaa'; // TS2322: Type 'string' is not assignable to type 'number'.

function foo(num: number) {}
foo('aaa'); // TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.

Not sure if this is relevant, but I see the following messages during the build:

TypeError: The "path" argument must be of type string. Received undefined
TypeError: Cannot read properties of undefined (reading 'includes')

Expected behavior

The webpack build raises errors such as:

ERROR in ./index.ts:1:7
TS2322: Type 'string' is not assignable to type 'number'.
  > 1 | const num: number = 'aaa';
      |       ^^^
    2 |
    3 | function foo(num: number) {}
    4 | foo('aaa');

ERROR in ./index.ts:4:5
TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
    2 |
    3 | function foo(num: number) {}
  > 4 | foo('aaa');
      |     ^^^^^
    5 |

Steps to reproduce the issue

  1. Clone the repository described below.
  2. npm ci
  3. npx webpack

In addition, it works fine if you exec npm i [email protected] before the build.

Issue reproduction repository

https://github.com/kku39/issue-repro-of-fork-ts-checker-webpack-plugin

Environment

  • fork-ts-checker-webpack-plugin: 9.0.2
  • typescript: 5.7.3
  • eslint: None
  • webpack: 5.98.0
  • os: Windows 11, debian:11

kku39 avatar Feb 17 '25 01:02 kku39

I believe this is ~caused by https://github.com/microsoft/TypeScript/issues/60500

JasonKleban avatar Feb 24 '25 13:02 JasonKleban

I made a PR (#865) to fix this by just returning undefined from the getModifiedTime function when the path is not a string. As far as I can tell this works fine. Type checking works properly, type checking errors are reported, and the TypeError messages during build are gone.

Until that PR is merged, I'm just fixing this with patch-package, which you can do like this:

  1. Make sure fork-ts-checker-webpack-plugin is pinned at 9.1.0 in your package.json
    "fork-ts-checker-webpack-plugin": "9.1.0",
    
  2. Install patch-package:
    npm install patch-package
    
  3. Add it to the post-install script in package.json:
    "scripts": {
    +  "postinstall": "patch-package"
    }
    
  4. Add the following patch as patches/fork-ts-checker-webpack-plugin+9.1.0.patch (copy the entirety of this text into that file):
    diff --git a/node_modules/fork-ts-checker-webpack-plugin/lib/typescript/worker/lib/system.js b/node_modules/fork-ts-checker-webpack-plugin/lib/typescript/worker/lib/system.js
    index 64a87b2..ff12715 100644
    --- a/node_modules/fork-ts-checker-webpack-plugin/lib/typescript/worker/lib/system.js
    +++ b/node_modules/fork-ts-checker-webpack-plugin/lib/typescript/worker/lib/system.js
    @@ -72,6 +72,9 @@ exports.system = Object.assign(Object.assign({}, typescript_1.typescript.sys), {
                 .map((dirent) => dirent.name);
         },
         getModifiedTime(path) {
    +        if (typeof path !== "string") {
    +            return undefined;
    +        }
             const stats = getReadFileSystem(path).readStats(path);
             if (stats) {
                 return stats.mtime;
    
  5. Run npm install which should apply the patch. Alternatively, run npx patch-package to apply it.

mogzol avatar May 21 '25 00:05 mogzol

hi, has solved now?

YYL1999 avatar Sep 02 '25 08:09 YYL1999

experiencing the same issue with 7.3.0

damirbogdanov avatar Sep 10 '25 02:09 damirbogdanov

still present in 9.1.0

clarejor avatar Oct 28 '25 16:10 clarejor