Type annotation in TypeScript

It's worth mentioning again that nothing looks different in the Hello World example we just saw. Let's employ some TypeScript specific features, one of which is Types. Types are the reason TypeScript exists, and every other feature apart from Types is just syntactic sugar.

We are not going into details with Types because Chapter 3, Typescript Native Types and Accessors, covers that. What we can discuss is Type annotation, which is a mechanism that TypeScript uses to apply strict types to a member. Annotation is achieved by following the member initialization with a colon (:) and the type (for example, string) as follows:

var firstName: string;

Let's have a look at some annotated examples:

var name: string = 'John'; ...

Get TypeScript 2.x for Angular Developers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.