安装 webpack-dev-server npm 包时不安全的打字稿编译 [英] Not safe typescript compilation when webpack-dev-server npm package is installed
问题描述
我有一个用 typescript 编写的非常小的项目,它目前是用 Babel 编译的.我想摆脱 Babel,只留下一个打字稿编译器来生成结果 JS.另外,我想让 JS 在移动和旧的桌面浏览器上工作,所以我保留默认目标设置 - 'ECMA Script 3'.
I have a very small project written in typescript and it is currently compiled with Babel. I want to get rid of Babel and leave just a typescript compiler to produce the result JS. Also, I want to have JS working on mobile and old desktop browsers, so I leave a default target setting - 'ECMA Script 3'.
为了演示我的问题,用一行代码编写一个脚本就足够了:
To demonstrate my issue, it's enough to complie a script with one line of code:
let clone = Object.assign({}, {a:1});
最初,编译器成功捕获问题并显示错误:
Initially, compiler successfully catches the issue and shows an error:
test.ts:1:20 - error TS2339: Property 'assign' does not exist on type 'ObjectConstructor'.
但是一旦我安装了 ;webpack-dev-server' npm 包,编译器就不会看到这个问题,并且没有错误地完成了编译.
But as soon as I install ;webpack-dev-server' npm package, compiler stops seeing this problem and completes the compilation with no errors.
我的小调查表明问题是由我的 node_modules 中的@types/node"包引起的,因为它引用了 es2018.
My little research showed that the problem is caused by '@types/node' package in my node_modules as it brings references to es2018.
在 node_modules>@types>node>ts3.2>index.d.ts 我看到:
In node_modules>@types>node>ts3.2>index.d.ts I see:
/// <reference lib="es2018" />
显然,Object.assign 是 ES2018 的有效方法,这就是编译器停止抱怨的原因.
Apparently, Object.assign is a valid method for ES2018 and that's why compiler stops complaining.
我安装这个包的原因是它是对以下路径的传递依赖:
The reason I have this package installed as it is a transitive dependency on the following path:
webpack-dev-server -> del -> @types/glob -> @types/node
是否可以将webpack-dev-server"npm 添加到我的项目中并让 ts 编译器捕获Object.assing"问题,这给我留下了一个问题?
It leaves me with a question if it is possible to have 'webpack-dev-server' npm added to my project and have ts compiler catching 'Object.assing' problem?
推荐答案
您可以在 tsconfig.json
文件中手动配置 types
属性.
You can manually configure types
property in your tsconfig.json
file.
最简单的形式:
{
"compilerOptions": {
"target": "es3",
"types": []
}
}
参见@types
、typeRoots
和 types
了解更多详情.
See @types
, typeRoots
and types
for more details.
这篇关于安装 webpack-dev-server npm 包时不安全的打字稿编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!