When you’re getting started with typescript you’re probably going to run into a bunch of warnings and errors. Pay attention to the messages, I’ve found them to be almost entirely correct. However you will see errors of the form

The name $ does not exist in the current scope

Why that’s jQuery, why doesn’t typescript know about jQuery? Well typescript doesn’t know about anything other than what’s in the current file. It is a dumb as a muffin. A tasty, tasty cranberry orange muffin. Maybe it has that streusel toping on it… sorry, I really like muffins. What can you do to solve this problem? Well that’s where definition files come into play. Definition files are, in effect, C include files. They define the public interfaces of the libraries you’re trying to use. Libraries like jQuery and d3.js. Libraries like your own libraries.

If you’re referencing your own typescript file then you don’t need to create a declaration file. Typescript can read its own typescript files so there is no need to generate declaration for them unless you’re distributing the declaration to other developers to develop against. To instruct the transcompiler to make use of either an external typescript file or declaration file you can include a reference in your typescript file.

/// ///

  • *You can see here that I pulled in both a declaration file for jQuery and a typescript file I created.

If you’re looking for a declaration file for a publicly distributed library then there are some great options. You can manually download it from Boris Yankov’s github repo at https://github.com/borisyankov/DefinitelyTyped. Or you can grab a package off nuget. If you’re using node then you can install the typescirpt definition tool from http://www.tsdpm.com/ and install packages using.

tsd install node

To generate your own definition files from a typescript file you can pass –declaration to the transcompiler. This will, unfortunately  only generate from typescript files. If you want to generate declarations from pure javascript then you’ll need to do it by hand.