Originally published on my website
I’ve been moving a few utilities that I use in multiple projects to npm libraries. But I needed an easy, reliable way to generate TypeScript declarations, since I primarily use TypeScript.
1. Open your project and ensure you have a
2. Install the
typescript library as a development dependency
pnpm i -D typescript
npm i -D typescript
yarn add typescript -D
3. Add the
prepare script to the
scripts object in
For example, mine looks like this:
"prepare": "tsc --declaration --emitDeclarationOnly --allowJs index.js"
This command runs
tsc, the TypeScript compiler, and tells it to only generate
.d.ts files (declaration files). Be sure to replace
pnpm run prepare
npm run prepare
yarn run prepare
If you have any questions, send a tweet my way. I hope that this guide comes in handy for you, thanks for reading!