Chapter 6. Declaration Files and Library Integrations

So far, we have been writing TypeScript without integrating with any external JavaScript libraries. We have covered the different concepts that the language adds on top of JavaScript and how to use them to our advantage when building large scale applications. We have ignored all of the third-party libraries that are openly available on the Web to improve the JavaScript experience. In this chapter, we will cover declaration files and how they help us integrate with other JavaScript libraries. The topics we are going to cover in this chapter include:

  • Declaration files
  • The NuGet package manager
  • jQuery
  • Knockout
  • External modules

Declaration files

Declaration files are a special type of source file in TypeScript and have a different file extension. Declaration files have a file extension of .d.ts and they can contain type information but no implementation details. This includes interface definitions as well as type declarations.

Type declarations are created using the declare keyword, as shown in the following screenshot:

Declaration files

As you can see, the name of the file is Book.d.ts, which is how the compiler knows that this file will only contain declarations. When this occurs, no output file is created and an error will be generated when the implementation code is found in the file. The purpose of these files is to provide type information for other JavaScript libraries that are not in TypeScript files. This allows us to interact with these libraries in a strongly typed fashion, providing compile time checks and intelligent code completion. In the previous example, we declare that the Book class will exist in the global namespace allowing it to be referenced throughout our project. We can now use Book freely, but if an implementation is not provided for it our application will fail during execution wherever the Book type is referenced.

As we saw earlier on when looking at the TypeScript compiler and its options, we can automatically generate declaration files for our own code. These declaration files can be referenced from other projects within your own solution or deployed as part of a library you distribute. A declaration file will be output for every JavaScript file that is output. If you combine all of your code into a single file then a single declaration file will be created. Selecting the Generate declaration files option from the TypeScript build section of the project properties will provide the compiler with the appropriate parameter used to create our declaration files, as shown in the following screenshot:

Declaration files

Throughout this book, we have used a variety of types that weren't explicitly declared by us. The CanvasRenderingContext2D object we used in our drawing application had a full list of type information in Visual Studio IntelliSense, yet we never saw its declaration or implementation. This is because TypeScript comes with a lib.d.ts file that declares type information for thousands of different objects. This declaration file is automatically referenced every time the compiler runs. This list of types is incredibly useful but it doesn't include any types from external libraries that may or may not be referenced in your projects.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset