Typescript Deep Dive by Basarat Ali Syed
Typescript Deep Dive by Basarat Ali Syed
Typescript Deep Dive by Basarat Ali Syed
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
TypeScript <strong>Deep</strong> <strong>Dive</strong><br />
might want to leave // TODO: comments as you go along.**<br />
Third Party JavaScript<br />
You can change your JavaScript to TypeScript, but you can't change the whole world to use JavaScript. This is where<br />
TypeScript's ambient definition support comes in. In the beginning we recommend you create a vendor.d.ts (the .d.ts<br />
extension specifies the fact that this is a declaration file) and start adding dirty stuff to it. Alternatively create a file specific<br />
for the library e.g. jquery.d.ts for jquery.<br />
Note : Well maintained and strongly typed definitions for nearly the top 90% JavaScript libraries out there exists in an<br />
OSS Repository called DefinitelyTyped. We recommend looking there before creating your own definitions as we<br />
present here. Nevertheless this quick and dirty way is vital knowledge to decrease your initial friction with<br />
TypeScript**.<br />
Consider the case of jquery , you can create a trivial definition for it quite easily:<br />
declare var $: any;<br />
Sometimes you might want to add an explicit annotation on something (e.g. JQuery ) and you need something in type<br />
declaration space. You can do that quite easily using the type keyword:<br />
declare type JQuery = any;<br />
declare var $: JQuery;<br />
This provides you an easier future update path.<br />
Again, a high quality jquery.d.ts exists at DefinitelyTyped. But you now know how to overcome any JavaScript -><br />
TypeScript friction quickly when using third party JavaScript. We will look at ambient declarations in detail next.<br />
JS Migration Guide<br />
56