Not safe typescript compilation when webpack-dev-server npm package is installed

江枫思渺然 提交于 2020-01-04 06:14:48

问题


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'.

But as soon as I install ;webpack-dev-server' npm package, compiler stops seeing this problem and completes the compilation with no errors.

My little research showed that the problem is caused by '@types/node' package in my node_modules as it brings references to es2018.

In node_modules>@types>node>ts3.2>index.d.ts I see:

/// <reference lib="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

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?


回答1:


You can manually configure types property in your tsconfig.json file.

In it's simplest form:

{
    "compilerOptions": {
        "target": "es3",
        "types": []
    }
}

See @types, typeRoots and types for more details.



来源:https://stackoverflow.com/questions/57643929/not-safe-typescript-compilation-when-webpack-dev-server-npm-package-is-installed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!