5

When I installed a non popular module that doesn't have a published type declaration under @types namespace, I'm stuck. I know how to write a declaration file, but I don't know how to make typescript compiler aware of it. The typescript documentation doesn't mention how to do it. The docs only mention to search for type declaration and if not found, publish a new one but doesn't mention how to do it without publishing.

So, say I installed a package named xyz and I know the public api that it exposes. Then, I write a type declaration file that matches the exposed api. Then, How to make typescript compiler aware of it?

2 Answers 2

4

Just add a <module-name>.d.ts file to your project, and TypeScript should become aware of it automatically. You don't need to import it or anything, just to be in the folders processed by TypeScript

Sign up to request clarification or add additional context in comments.

5 Comments

Could you be more specific, where should I put that file? Say, all my typescript codes is under src/ directory, where should I put that file?
Notice: In the definition file, the author must wrap the exported members in: declare module "xyz" { ... }.
@MasBagol, put it under src/, yes. You can also add a folder types under you src if you want, it doesn't matter. And no, you don't need to 'export' anything, as it is an ambient declaration file.
Ah, okay. Thanks :)
Very important that the file begin with ``` declare module '<module name>'; ```
0

Does your project include a tsconfig.json file? If so you can specifiy paths to type definitions under the compilerOptions:

{
   "compilerOptions": {
       "typeRoots" : ["./typings"]
   }
}

About 'tsconfig.json' in the TypeScript handbook

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.