Lesson 8 of 60 – Type Annotations
13%

Type Annotations in TypeScript

Type annotations are one of the most important features of TypeScript. They allow us to explicitly specify the type of data that a variable, function parameter, object property, or function return value should use.

Note: A type annotation is written after the variable or parameter name using a colon :.

1. What is a Type Annotation?

A type annotation tells TypeScript what type of value is expected.

let name: string = "Rahul";

Here, string is the type annotation. It tells TypeScript that name should contain text.

2. Type Annotation Syntax

The basic syntax is:

let variableName: type = value;

For example:

let age: number = 20;

The colon separates the variable name from its type.

3. String Type Annotation

Use string when a variable should contain text.

let studentName: string = "Amit";
let city: string = "Aurangabad";

Both variables are declared as strings.

4. Number Type Annotation

Use number for numeric values.

let age: number = 21;
let marks: number = 85;
let price: number = 500;

All three variables are expected to contain numbers.

5. Boolean Type Annotation

Use boolean for true or false values.

let isStudent: boolean = true;
let isPassed: boolean = false;

6. Type Annotation Prevents Wrong Assignment

If a variable is declared with a type, TypeScript can detect an incompatible assignment.

let age: number = 20;

age = "Twenty";

The second line causes a type error because age is a number.

7. Type Annotation with const

Type annotations can also be used with const.

const country: string = "India";
const year: number = 2026;

8. Type Annotation for Arrays

You can specify the type of values that an array should contain.

let marks: number[] = [80, 75, 90, 85];

This means the array is intended to contain numbers.

Another syntax is:

let names: Array<string> = [
    "Amit",
    "Rahul",
    "Priya"
];

9. String Array

A string array can be declared using:

let students: string[] = [
    "Amit",
    "Rahul",
    "Priya"
];

Every element is expected to be a string.

10. Boolean Array

You can also create an array containing boolean values.

let results: boolean[] = [
    true,
    false,
    true
];

11. Type Annotation for Function Parameters

Function parameters can also have type annotations.

function greet(name: string) {
    console.log("Hello " + name);
}

The name parameter is expected to be a string.

12. Multiple Function Parameters

You can specify types for multiple parameters.

function add(
    a: number,
    b: number
) {
    return a + b;
}

Both parameters are expected to be numbers.

13. Function Return Type Annotation

You can specify the type of value returned by a function.

function add(
    a: number,
    b: number
): number {

    return a + b;
}

The : number after the parameter list specifies the return type.

14. String Return Type

A function returning text can use string.

function getMessage(): string {
    return "Hello TypeScript";
}

15. Boolean Return Type

A function returning true or false can use boolean.

function isAdult(age: number): boolean {
    return age >= 18;
}

16. void Return Type

If a function does not return a value, you can use the void type.

function showMessage(
    message: string
): void {

    console.log(message);
}

The function performs an action but does not return a value.

17. Object Type Annotation

You can define the expected structure of an object.

let student: {
    name: string;
    age: number;
} = {
    name: "Rahul",
    age: 20
};

18. Object with More Properties

let student: {
    id: number;
    name: string;
    age: number;
    active: boolean;
} = {

    id: 101,
    name: "Amit",
    age: 21,
    active: true

};

Each property has its own type annotation.

19. Function Parameter with Object Type

An object type can also be used as a function parameter.

function showStudent(
    student: {
        name: string;
        age: number;
    }
): void {

    console.log(student.name);
    console.log(student.age);
}

20. Tuple Type Annotation

A tuple allows you to define the types and order of elements.

let student: [string, number] = [
    "Rahul",
    21
];

The first value is a string and the second value is a number.

21. Union Type Annotation

A union type allows a value to have more than one possible type.

let studentId: number | string;

studentId = 101;
studentId = "ST101";

The | symbol means "or".

22. Literal Type Annotation

A literal type can restrict a value to specific literal values.

let direction: "left" | "right";

direction = "left";
direction = "right";

Only the specified values are allowed.

23. Type Annotation with null

TypeScript can explicitly work with null.

let value: null = null;

Here the variable is specifically assigned the null value.

24. Type Annotation with undefined

You can also explicitly specify the undefined type.

let value: undefined = undefined;

This is useful when you specifically need the value to be undefined.

25. Type Annotation for API Data

Type annotations are especially useful when working with data received from an API.

let user: {
    id: number;
    name: string;
    email: string;
} = {
    id: 101,
    name: "Rahul",
    email: "rahul@example.com"
};

The structure of the expected user data is clearly defined.

26. Type Annotation in React Native

Type annotations are very useful in React Native development. For example, you can type component data:

interface User {
    id: number;
    name: string;
}

const user: User = {
    id: 101,
    name: "Rahul"
};

Later, these concepts will be used for React Native props, state, navigation, and API responses.

27. Type Annotation vs Type Inference

With a type annotation, you explicitly specify the type:

let age: number = 20;

With type inference, TypeScript determines the type automatically:

let age = 20;

Both approaches are useful. Type annotations are helpful when you want to explicitly describe the expected type.

28. Complete Student Example

let studentId: number = 101;
let studentName: string = "Amit";
let marks: number = 88;
let passed: boolean = true;

function showStudent(
    id: number,
    name: string,
    marks: number,
    result: boolean
): void {

    console.log("ID:", id);
    console.log("Name:", name);
    console.log("Marks:", marks);
    console.log("Passed:", result);
}

showStudent(
    studentId,
    studentName,
    marks,
    passed
);

This example uses type annotations for variables and function parameters.

29. Common Type Annotation Mistakes

Mistake 1: Assigning the wrong type.

let age: number = "Twenty";

Mistake 2: Passing the wrong type to a function.

function add(a: number, b: number) {
    return a + b;
}

add(10, "20");

Always make sure the value matches the declared type.

30. Type Annotation Summary

Type Example
string let name: string = "Rahul";
number let age: number = 20;
boolean let active: boolean = true;
array let marks: number[] = [80, 90];
tuple let data: [string, number] = ["Amit", 20];
union let id: number | string;
void function show(): void { }
object let user: { name: string };
Remember: Type annotations are written using a colon followed by the type. They help TypeScript understand what kind of data your program expects.

📌 Key Points

  • Type annotations explicitly specify data types.
  • A colon : is used before the type.
  • string is used for text.
  • number is used for numbers.
  • boolean is used for true or false values.
  • Arrays can have type annotations.
  • Functions can have typed parameters and return types.
  • Objects can have typed properties.
  • Union types allow more than one specified type.
  • Type annotations are very useful in React Native development.

🧠 Quick Quiz

Question: Which symbol is used to specify a type annotation in TypeScript?