5 common ways of Type Annotation in TypeScript

Daminda Dinesh Imaduwa Gamage
4 min readFeb 24, 2023
Photo by Christin Hume on Unsplash

What is Type Annotation?

TypeScript is a statically-typed language. Hence, Type Annotation is an essential part of TypeScript that you need to be well familiar with. Type Annotations mean different ways that you can define types of variables, parameters, and return values of functions, and properties (of objects).

There are many ways to do type annotation in TypeScript. Some are more common than others. We are going to discuss the most common ways of type annotation.

Explicit type annotation

This is probably the most common way to do it. Let’s see how to use explicit annotation on variables using some of the primitive data types in TypesScript.

let firstName: string = "John";
let age :number = 32;
var isPrimeNumber : boolean = false;
const COUNTRY : string = "United States";

Note: You can also define type by inference.

Interfaces for type annotation

Interfaces are another way of doing type annotations. Interfaces can be used to declare object types as follows.

interface Person {
firstName : string;
age : number;
}

//Then you can use the Person object type to define a variable as follows.
let friend…

--

--