TypeScript: Specify a directory to search for module type definitions

Heyho,

I want to use some javascript libraries in my typescript code for which there is no typing in npm. So I wrote the typing myself and put them in a directory definitions

in the source tree. However, I could not get typescript to search this directory for these modules.

My directory structure looks like this:

+-node_modules
| |
| +-moduleA
| |
| +-moduleB
|
+-src
| |
| +-definitions
| | |
| | +-moduleA.d.ts
| | |
| | +-moduleB.d.ts
| |
| +-ts
|   |
|   + ... all typescript code ...
|
+-tsconfig.json

      

I tried to include modules in the directory definitions

using

  • include

  • files

  • typeRoots

  • paths

However, none of this worked.

Can anyone tell me how to get typescript to enable these typings?

PS: Why does the typescript module work so hard?

+3


source to share


2 answers


{
  "compilerOptions": {
    "typeRoots": [
      "./node_modules/@types",
      "./some-custom-lib"
    ]
  }
}

      



the array of strings typeRoots is used for this. In addition to your usual "node_modules / @ type" add a typed folder.

+3


source


You can enable them with triple slash directives to direct the compiler to use them.

eg. create a file index.d.ts

and place it in the definitions folder. There you can enable every custom text input you have made. It might look like this.

/// <reference path="react.patch.d.ts" />
/// <reference path="custom-typings.d.ts" />

      

Inside the typing file, the first line should be



/// <reference types="nameOfIt" />

      

Then in yours tsconfig.json

you include them in a box files

, for example.

"files": [
    "definitions/index.d.ts"
  ]

      

0


source







All Articles