Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: Specify a directory to look for module type definitions

Heyho,

I want to use some javascript libraries in my typescript code for which there are no typings in npm. So I wrote the typings myself and put them in a definitions directory in my source tree. However, I could not get typescript to look in that directory for those 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 including the modules in the definitions-directory using

  • include
  • files
  • typeRoots
  • paths

However none of it worked.

Can somebody tell me, how to get typescript to include those typings?

PS: Why is the TypeScript module handling so complicated???

like image 844
jsf Avatar asked Aug 04 '17 12:08

jsf


People also ask

Where does TypeScript look for type definitions?

TypeScript automatically finds type definitions under node_modules/@types , so there's no other step needed to get these types available in your program.

What is @types in Node_modules?

This @types scoped package is where we can find a ton of useful type definitions, such as for example the type definitions of node that allow us to use require for example.


1 Answers

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

the typeRoots string array is just for that. In addition to your regular "node_modules/@type", add a custom made typings folder.

like image 88
qballer Avatar answered Sep 19 '22 14:09

qballer