Type annotations

One of the most commonly used features of TypeScript is its type annotations, where variables are annotated with the data type of the information they contain.

With type annotations
var greeting: string = "Hello world!"
const pi: number = 3.1415926535
let found: boolean = false

By adding type annotations, TypeScript can use type inference to detect a wide variety of situations where a valid JavaScript program would encounter runtime errors.

Loading TypeScript...

Notice that even without type annotations, TypeScript will use type inference to determine the data types of variables and functions.

There is also a special type any which can be applied to a variable whose data type is unknown. This means TypeScript will be unable to detect an error in the same program as above, but the program will produce a runtime error when it is executed.

Loading TypeScript...

TypeScript helps you prevent runtime errors by exposing a wide range of potential issues as compilation errors, which can be addressed during the development process rather than as a bug fix later on.

Was this page helpful?