Example of typescript types package.
To use our types as package we need to compile it to the .d.ts
a.k.a declarations and then push it to the registry.
-
Setup
tsconfig.json
"declaration": true
- activating declarations in our project."emitDeclarationOnly": true
- allows to get only typings from our code."declarationDir": "./dist"
- directory for declarations output
-
Create
index.ts
outsidesrc
folder.Allows us to export all code from src.
-
Build
tsc
- Add
.ts
or.d.ts
files to thesrc
folder. - Run
build
script from package.json or via command line with your package manager:
npm run build
yarn build
pnpm build
- Inspect compiled types declarations in the
dist
folder.
I added files for npm registry. You can safely delete it or replace.
- Login:
npm login
- Publish:
npm publish --access public
NOTE: Sometimes you need to provide login token or something similar. Remember to delete it from your project.