Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure tsconfig.json for typings not in @types?

I'm using a tsconfig.json file to specify which typings I want to use in my app.

{
   "compilerOptions": {
       "types" : ["node", "lodash", "express"]
   }
}

This imports typings from ./node_modules/@types/node, ./node_modules/@types/lodash and ./node_modules/@types/expres.

My question is how can I configure typings for self-contained modules?

My example is the zone.js package which has both the library code and type definitions.

  • ./node_modules/zone.js/dist/zone.js
  • ./node_modules/zone.js/dist/zone.min.js
  • ./node_modules/zone.js/dist/zone.js.d.ts

What do I put in my tsconfig.json file to include zone.js.d.ts?

like image 366
Steven Liekens Avatar asked Feb 09 '18 10:02

Steven Liekens


People also ask

What is typeRoots in Tsconfig json?

'typeRoots' specifies root folders in which the transpiler should look for type definitions (eg: 'node_modules').

Which of the following option is correct about Tsconfig json?

Correct Answer : Option (C) : This file is used to give the options about TypeScript used for the Angular JS project.

What is Lib in Tsconfig json?

with --lib you can specify a list of built-in API declaration groups that you can chose to include in your project. For instance, if you expect your runtime to have support for Map, Set and Promise (e.g. most evergreen browsers today), just include --lib es2015.


2 Answers

You just have to add zone.js to the types in your tsconfig.json:

{
   "compilerOptions": {
       "types" : ["node", "lodash", "express", "zone.js"]
   }
}

Note that you do not have to include all types like this. Type definitions from the @types/* packages are automatically included.

So you could remove the types declaration in your tsconfig.json and all the @types/* packages would be automatically referenced.

In order to get zone.js to work you can either include it in a single file like this:

/// <reference types="zone.js" />

Or if you want it available in your whole project you can add a index.d.ts at the root of your project and put int the reference there.

like image 77
a-ctor Avatar answered Oct 19 '22 16:10

a-ctor


You can place the file anywhere, but you need to tell the compiler about it, either by adding it in tsconfig.json or as a ///<reference>. For tsconfig add the include field:

{
    "compileOnSave": false,
    "compilerOptions": {
        ..
    },
    "include": [
        "zone.d.ts",
    ]
}
like image 10
Titian Cernicova-Dragomir Avatar answered Oct 19 '22 17:10

Titian Cernicova-Dragomir