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: Person = { firstName: 'john', age:70 }

Type aliases for type annotation

Type Aliases let you have a custom name for a type. Let’s see how to work with type aliases.

type mystring = string;
let country:mystring = "USA";

You can also alias object types

type Student = {
firstName: string,
age: number

// now , you can use the alias 'Student' to create object of that type
let californiaStudents: Student = { firstName:'john', age: 23 };

You can do the above code as follows if you use an interface to declare an Object type.

interface Person{
firstName : string;
age : number;

type Student = Person;
let californiaStudents: Student = { firstName:'john', age: 33 };

TypeScript Generics