網站搜尋

typescript - 在使用 Webpack 的專案上執行 `tsc` 時會拋出在 webpack 中未見的錯誤


我使用 Webpack 來構建我的 Typescript 項目。然而,隨著我的項目變得越來越大,Webpack 的速度變慢了。

我想把 TypeScript 的檢查拆分成一個使用 tsc 的獨立過程。

然而,當我在我的專案中執行 tsc 時,我收到了一些型別宣告錯誤,而使用 Webpack 的 ts-loader 時則不會出現這些錯誤。

我認為這是由於 TypeScript 和 Webpack 在導入模組上的差異所造成的。

模組解析:非常重要的是要意識到這個插件使用的是 TypeScript 的模組解析,而不是 webpack 的模組解析。這意味著你必須正確設定 tsconfig.json。例如,如果你在 tsconfig.json 中設置了 files: ['./src/someFile.ts'],這個插件將只檢查 someFile.ts 的語義錯誤。這是出於效能考量。這個插件的目標是儘可能地快速。使用 TypeScript 的模組解析,我們不需要等待 webpack 編譯文件(webpack 在編譯過程中會遍歷依賴圖),我們從一開始就擁有完整的文件列表。https://github.com/Realytics/fork-ts-checker-webpack-plugin#modules-resolution

以下是一些範例錯誤:

node_modules/@types/node/index.d.ts:143:13 - error TS2300: Duplicate identifier 'require'.
node_modules/@types/react-native/index.d.ts:8719:14 - error TS2300: Duplicate identifier 'require'.
node_modules/@types/react-native/index.d.ts:8745:18 - error TS2717: Subsequent property declarations must have the same type.  Property 'geolocation' must be of type 'Geolocation', but here has type 'GeolocationStatic'.
node_modules/@types/webpack-env/index.d.ts:203:13 - error TS2300: Duplicate identifier 'require'.
node_modules/rxjs/scheduler/VirtualTimeScheduler.d.ts:24:15 - error TS2416: Property 'work' in type 'VirtualAction<T>' is not assignable to the same property in base type 'AsyncAction<T>'.

更多細節請見這裡: https://gist.github.com/IanEdington/b1567b2cabae262eac8a6ef6a4206d4b

答案#1

感謝 @qDot 找出這個問題的解決方法:https://github.com/Realytics/fork-ts-checker-webpack-plugin/issues/116#issuecomment-385286544

這是 node 和 typescript 函式庫衝突的問題。參見:

https://github.com/Microsoft/TypeScript/issues/23769

快速解決方法:降回 @types/node v9.6.7,直到 v10 可以修復。