Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is @types/core-js still necessary for TS typings in a Node.js project?

I have several backend-only Node.js projects which use a simple TypeScript config.

Before March 2018, I had this in package.json:

  "devDependencies": {
    "@types/core-js": "^0.9.46",
    "@types/node": "^9.6.2"
  }

since ~March 2018, I have been omitting "@types/core-js" and everything seems to compile fine. Do we need "@types/core-js" anymore?

like image 306
Alexander Mills Avatar asked Jun 17 '18 03:06

Alexander Mills


2 Answers

I have several Node-based projects written in TypeScript. I never ever used @types/core-js in my projects. So it is definitely not necessary to use @types/core-js in order to write Node-based code. Having @types/node, on the other hand, is a boon if you're going to use Node's API.

The first thing to do if you run into a compilation issue that appears to be caused by the compiler not knowing about features introduced in ES6 is to set lib to load the proper typings you need. For instance, if you decide to target es5 but want to use methods, classes, functions introduced in ES6, you do need to specify "es6" in your lib setting because by default the target es5 loads the lib es5 too. (According to the documentation, a target of es5 sets lib by default to DOM,ES5,ScriptHost, whereas a target of es6 sets lib by default to DOM,ES6,DOM.Iterable,ScriptHost.)

I've run into some very rare cases where there's something missing in the es6 lib. In such cases, I just write a definition to fix that specific problem. Loading @types/core-js might also fix the issue, but it also contains a lot of other things that are not necessary, and it adds a dependency to the project, which then must be managed, etc. Not worth it.


The focus above was Node projects, but even in web-based projects where I have a dependency on core-js I don't use @types/core-js. As I see it, the presence of core-js in a project should be entirely transparent to the project's application code. I'm writing code made to run on any platform providing an API that conforms to ES6 or later, I'm not writing code made to run with core-js specifically.

like image 170
Louis Avatar answered Nov 16 '22 02:11

Louis


You should only need @types/core-js if you're using something that is in that declaration that is not available in your TypeScript declarations for your major version. This is useful if you're expecting to use core JavaScript ES6 features that are in core-js but were not implemented in the version of TS you were using at the time. There's more information available on the TS GitHub related to your question but in all reality if you're just using ES stuff that's there now, no you shouldn't need it anymore.

edit: Before I had stated it as node, but in all possibilities since it's TS it probably didn't know how to compile to the target without the declaration. As of TS 2 and NodeJS 6 you shouldn't really need it anymore.

like image 26
Robert Mennell Avatar answered Nov 16 '22 03:11

Robert Mennell