How to properly build and package a TypeScript library so that it can be used from both JavaScript and TypeScript code easily using Bower and TSD?
Asked
Active
Viewed 977 times
1 Answers
2
Compile your TypeScript sources to both JavaScript and TypeScript Declaration files in your build script:
tsc <source> <dependencyDeclarations> --out <targetJavaScript> --declaration
Where <dependencyDeclarations>
are the .d.ts
files of your dependencies, conveniently typings/tsd.d.ts
if you use TSD.
So this command will generate a .js
and a .d.ts
file in the same folder with the same name. Let's call the latter <targetDeclaration>
. Specify both in your bower.json
:
{
"main": "<targetJavaScript>",
"typescript": {
"definition": "<targetDeclaration>"
}
}
(You can also ignore every other files.) Then publish your package using Bower as usual.
To use the library, run:
bower install <library> --save
tsd link
TSD 0.6 will detect and include the specified <targetDeclaration>
.

thSoft
- 21,755
- 5
- 88
- 103
-
1Plus one. One more tip: look at https://github.com/TypeStrong/dts-bundleneeded to generate correct `.d.ts` files if you use `external modules` – basarat Jan 13 '15 at 09:56
-
1What about other declarations that your library is using? like jQuery. – gilamran Jan 30 '15 at 20:39