What's the difference from installing type def

2020-07-14 12:06发布

I've been using the command line and installing TypeScript type definition files .d.ts using Typings from the command line like the following:

typings install dt~jquery --global --save

This works perfectly and if done at the root of my project, it gets installed to the typings/globals directory.

I recently had seen tutorials adding typings installations via the devDependencies section in the package.json file for npm to install like the following:

  "devDependencies": {
    "@types/core-js": "0.9.34"
  }

I actually did the above and couldn't even find where the d.ts file was put as I didn't see it anywhere in the node_modules folder.

What is the difference between installing typings from the (2) methods above, and where do the typings files end up if using npm to install and pull down the files?

1条回答
劫难
2楼-- · 2020-07-14 12:57

Installing types with npm scoped package @types is the new official way in TypeScript 2.0. See What’s New in TypeScript 2.0? / Simplified Declaration File (.d.ts) Acquisition :

Typings and tsd have been fantastic tools for the TypeScript ecosystem. Up until now, these package managers helped users get .d.ts files from DefinitelyTyped to their projects as fast as possible. Despite these tools, one of the biggest pain points for new users has been learning how to acquire and manage declaration file dependencies from these package managers.

Getting and using declaration files in 2.0 is much easier. To get declarations for a library like lodash, all you need is npm:

npm install --save @types/lodash

The above command installs the scoped package @types/lodash which TypeScript 2.0 will automatically reference when importing lodash anywhere in your program. This means you don’t need any additional tools and your .d.ts files can travel with the rest of your dependencies in your package.json.

The typing files are installed inside the node_modules/@types directory. So the file you are looking for can be found here node_modules/@types/core-js/index.d.ts In fact they are simple node-modules, TS 2.0. know how to handle properly.

查看更多
登录 后发表回答