Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can typings auto install d.ts from package.json?

I install about twenty modules in project, and now I want to use typescript.But I need to install d.ts one by one. It's to boring. So is there a way for typings to install d.ts from package.json automatically?

like image 439
Lxxyx Avatar asked Jul 13 '16 05:07

Lxxyx


2 Answers

Proposed solution

You could use the typings tool, as SnareChops suggests, but in package.json have a "postinstall" script assigned to run the typings install command after installs, just be sure to add typings as a dev dependency as below:

{
  "name": "my-project",
  "version": "1.0.0",
  "description": "My project description",
  "main": "index.js",
  "author": "CodeAnimal",
  "scripts": {
    "postinstall": "typings install"
  },
  "devDependencies": {
    "typings": "^1.3.1"
  }
}

More info

With regards to finding typescript definitions automatically, this is what typings is for to some extent, as it searches many sources to find the definition file(s). Unfortunately, the definition file could be under a different name as the npm module name, which means automatic installs without the typings tool isn't entirely possible unless the creator of the package includes the definitions with the package (see below).

If you've used tsd before, you should now migrate over to typings.


For package creators

If you are creating an npm package and using TypeScript 1.6+, you can include the TypeScript definitions files in the package itself using the "typings" property in package.json:

{
    "name": "my-project",
    "main": "lib/index.js",
    "typings": "lib/index",
    "files": [
        "lib/"
    ]
    // etc
}

Just make sure you specify declaration as true in the tsconfig.json file for the TypeScript project:

{
    "version": "1.6.2",
    "compilerOptions": {
        "module": "commonjs",
        "declaration": true,
        "outDir": "lib/"
    },
    "files": [
        "./src/index.ts"
    ]
}

There is a great article on how to do this over at medium which is where I originally learnt about this.

If all package authors did this, then the typings tool would technically become redundant and it would just require all IDEs to use the typings package property to know where to access the definition files for a given module/package. Of course, it would require all authors to either use TypeScript (and generate definition files automatically) or for them to manually create definition files and keep them in sync with any code changes they make. It is because of the latter, that is that creators understandably don't create definition files manually for projects that aren't written in TypeScript, that tools like typings exists along with an active community to create these definitions for those that do want to use TypeScript.

like image 57
Code Animal Avatar answered Nov 08 '22 22:11

Code Animal


You can use the tool typings to create it's own typings.json file that you can save your typescript dependencies to.

Example:

typings install --save jquery

Then if another developer works on your project, they would only need to run

npm install && typings install to get everything needed to build.

like image 31
SnareChops Avatar answered Nov 08 '22 23:11

SnareChops