2

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?

davidoff
  • 2,175
  • 3
  • 16
  • 23

1 Answers1

2

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.

Nenad
  • 24,809
  • 11
  • 75
  • 93