I am new to Angular2 and I'm enjoying it so far but there is one problem: Typings. I can't figure out how to use them and what exactly they are. Some places say to use typings, others say to use npm to install definitions.
I'm confused!!
The . d. ts file is usually placed adjacent to the . ts file, it is providing the typings for.
Typings is an NPM package to handle the type definitions associated with third-party libraries. This way the tooling will be aware of the types used inside the application. After explaining the typings library, Scott summarizes the different coding syntax options with Angular 2.
Type Annotations are annotations which can be placed anywhere when we use a type. The use of Type annotation is not mandatory in TypeScript. It helps the compiler in checking the types of variable and avoid errors when dealing with the data types.
JavaScript is untyped, meaning that we can pass around and use data, objects and functions with no constraints. We can write code that calls methods that don't exist on an object, or reference variables that we don't have. These kinds of mistakes can be hard to discover when you are writing code, and it can lead to unstable and buggy code. Doing big changes of your code can become difficult and risky as you don't immediately see if some changes conflicts with the rest of the code somewhere else.
TypeScript is mainly about adding types to JavaScript. That means that TypeScript requires you to accurately describe the format of your objects and your data. When you do that, that means that the compiler can investigate your code and discover errors. It can see that you are trying to call a function with the wrong kinds of arguments, or reference a variable that is not accessible in the current scope.
When you write TypeScript yourself, this formal description of the code is part of the code itself.
However, when you use external libraries like jQuery or moment.js, there are no information of the types in that code. So in order to use it with TypeScript, you also have to get files that describe the types of that code. These are the type declaration files, most often with the file extension name .d.ts
. Fortunately people have written those kinds of type declaration files for most common javascript libraries out there.
Typings was just a tool to install those files. It is now best practice to just use npm.
When you have installed those files, which basically only means downloading them and placing them in your project, the TypeScript compiler will understand* that external code and you will be able to use those libraries. Otherwise you would only get errors everywhere.
* Depending on how you have set up your project and configured it, you might have to configure typescript to look for those files specifically, or it might just work without any configuration from your part.
What are typings?
In short, TypeScript adds static typing to JavaScript. Since JavaScript alone does not define such type definitions, they must be written and included in the TypeScript compilation process through additional mechanisms. In other words, a JavaScript library may define a function:
export function createPerson(name, age, initScore) { ... }
But in the statically typed environment of TypeScript, it would be much more useful for the developer to have something like this:
interface Person { name: string, age: number, score: number } export function createPerson(name: string, age: number, initScore?: number): Person;
The purpose of type declarations, often called typings, type definitions or just types, is to fill in these details over implementations, thus identifying API misuses even across libraries. They usually reside in files with the .d.ts
extension, and can be built from .ts
files using the compiler. When programs were not written in TypeScript however, these declarations are usually written manually.
With that said, your confusion is somewhat justified: more than one approach to installing and managing TypeScript declarations were made over time. Right now however, there is an official (and therefore recommended) means of installing and using declaration files, and it's documented here:
In TypeScript 2.0, it has become significantly easier to consume declaration files, in acquiring, using, and finding them. This page details exactly how to do all three
[...]
Getting type declarations in TypeScript 2.0 and above requires no tools apart from npm.
As an example, retrieving declarations for Angular is as simple as installing this dependency in your project:
npm install --save @types/angular
Packages in the @types
namespace will be automatically considered by the compiler for retrieving type declarations. Furthermore, if these declarations are already embedded in the npm package, you don't have to do anything else other than installing that package (an example of such a package is redux). For more information, see the docs on tsconfig.json. There is also a whole section about authoring your own declaration files, which is a recommended read to all developers wishing to add type declarations to their own libraries.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With