'manually copying types to @types/ directory during npm post-install

I would like to distribute a NPM package already containing type declarations which expand the global environment. (think of jest or mocha with a global test function)

However, I believe that typescript will only automatically include types if the module is located inside the node_modules/@types directory, which is not something I am looking for as I don't want to distribute types over there at the moment.

I am currently running a post install script with:

{"scripts": {"postinstall": "cd ../ && cp -r my-module @types/my-module"}}

In order to manually inject my types to the user global without extra effort.

Is this advisable or is there a better way to achieve this without asking the user to installing types separately or manually defining typeRoots?



Solution 1:[1]

In your package.json file add the following script:

"postinstall": "npx typescync"

Pre-req: npm install postinstall --save

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1