之前處理過 VS2017 更新導致 TypeScript 出現大量 is not assignable to 錯誤問題,若暫時不打算改寫 TypeScript 升級 2.4+,確保專案 TypeScript 版本設定維持在舊版即可。但實際使用發現有個困擾:雖然編輯及使用都正常,但 VS2017 的錯誤清單視窗(Error List)冒出大量 is not assignable to 錯誤,一片紅通通很礙眼,其他程式有錯要查訊息如同大海撈針。

研究一陣子不得其解,不經意看到最下方兩則 Warning 才有點頭緒:

Your project is built using TypeScript 2.3, but the TypeScript language service version currently in use by Visual Studio is 2.5. Your project may be using TypeScript language features that will result in errors when compiling with this version of the TypeScript compiler. To remove this warning, install the TypeScript 2.5 SDK or update the TypeScript version in your project's properties.

JavaScript Language Service 是 VS2017 推出的新服務,負責提供 JavaScript/TypeScript Intellisense,它會隨 VS2017 版本更新使用最新版 TypeScript Compiler,而專案編譯採用的 TypeScript Compiler 版本則由 .csproj 設定決定,因此就出現 Error List 認定有錯但編譯可過的矛盾現象。

爬文沒查到解法,腦海閃過偷改 TypeScript Language Service 編譯器版本設定的想法,但太髒太 Hacking 了,趕緊自打巴掌逼自己忘掉,最後乖乖調整 TypeScript 到 2.4+ 相容解決問題。所幸,靠外加 <any> 強轉型或是型別宣告 Union any 幾乎都能解決,還算好改,而改到相容到底才是治本之道。


Comments

Be the first to post a comment

Post a comment