Lesson 21 of 60 – Function Type Annotations
35%

Function Type Annotations in TypeScript

Type annotations can be used with functions to specify the types of parameters and the type of value returned by the function. This helps TypeScript identify incorrect values before the program runs.

Note: In TypeScript, you can specify the type of each function parameter and also specify the function's return type.

1. What are Function Type Annotations?

Function type annotations specify what types of values a function expects and what type of value it returns.

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

Here, both parameters are numbers and the function returns a number.

2. Typing Function Parameters

You can specify the type of every parameter after its name.

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

The name parameter must be a string.

3. Number Parameter

A function parameter can be declared as a number.

function square(value: number) {
    return value * value;
}

The function expects a numeric value.

4. String Parameter

A string parameter can be declared using string.

function showName(name: string) {
    console.log(name);
}

Passing a number instead of a string will cause a TypeScript type error.

5. Boolean Parameter

A parameter can also be declared as a boolean.

function checkStatus(active: boolean) {
    console.log(active);
}

The function expects either true or false.

6. Multiple Parameters

You can add type annotations to multiple parameters.

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

Both a and b must be numbers.

7. Different Parameter Types

Different parameters can have different types.

function studentInfo(
    name: string,
    age: number
) {
    console.log(name, age);
}

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

8. Return Type Annotation

A function's return type is written after the closing parenthesis.

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

The : number specifies the return type.

9. Function Returning a String

Use : string when the function returns a string.

function getName(): string {
    return "Rahul";
}

TypeScript expects the function to return a string.

10. Function Returning a Number

Use : number when the function returns a number.

function getAge(): number {
    return 25;
}

11. Function Returning a Boolean

Use : boolean when the function returns true or false.

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

12. Function Without a Return Value

A function that does not return a value can use the void return type.

function showMessage(message: string): void {
    console.log(message);
}

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

13. Calling a Typed Function

A typed function can be called by passing values that match its parameter types.

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

greet("Amit");

14. Type Error in Function Arguments

TypeScript reports an error when an incorrect type is passed to a function.

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

// Incorrect:
// greet(100);

The function expects a string.

15. Function with Three Parameters

Functions can have several typed parameters.

function student(
    name: string,
    age: number,
    active: boolean
): void {
    console.log(name, age, active);
}

16. Calculating Student Percentage

Function annotations are useful for application logic.

function percentage(
    marks: number,
    total: number
): number {
    return (marks / total) * 100;
}

Both parameters and the return value are numbers.

17. Function with an Array Parameter

An array can also be used as a typed parameter.

function total(numbers: number[]): number {
    return numbers.reduce(
        (sum, value) => sum + value,
        0
    );
}

18. Function with Object Parameter

An object type can be used as a function parameter.

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

19. Function Returning an Object

You can also specify an object as the return type.

function getStudent(): {
    name: string;
    age: number;
} {
    return {
        name: "Amit",
        age: 20
    };
}

20. Function with Union Type

A parameter can accept more than one type using a union type.

function showValue(
    value: string | number
): void {
    console.log(value);
}

The function accepts either a string or a number.

21. Function with null

A parameter can allow null using a union type.

function showName(
    name: string | null
): void {
    console.log(name);
}

The function accepts either a string or null.

22. Function with Optional Parameter

A parameter can be optional by placing ? after its name.

function greet(
    name?: string
): void {
    console.log(name);
}

The optional parameter can be omitted when calling the function.

23. Arrow Function Type Annotation

Arrow functions can also use parameter and return type annotations.

const add = (
    a: number,
    b: number
): number => {
    return a + b;
};

24. Function Type with React

Type annotations are useful when defining functions used inside React components.

function showMessage(
    message: string
): void {
    console.log(message);
}

This makes the expected parameter type clear.

25. Function Type with React Native

React Native applications frequently use functions for button presses, navigation, API calls, and other actions.

function handlePress(): void {
    console.log("Button pressed");
}

26. Typed Button Handler Example

A typed function can be used as a button event handler.

const handleSave = (): void => {
    console.log("Data saved");
};

The void return type indicates that the function does not return a value.

27. Why Function Annotations are Useful

Function annotations make code easier to understand and help detect incorrect values.

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

Anyone reading the function can immediately understand what values it expects and returns.

28. Common Mistake

A common mistake is assigning a value that does not match the parameter type.

function multiply(
    a: number,
    b: number
): number {
    return a * b;
}

// Incorrect:
// multiply("10", 5);

Both arguments should be numbers.

29. Complete Function Annotation Example

Here is a complete example using parameter and return type annotations.

function calculateTotal(
    price: number,
    quantity: number
): number {

    return price * quantity;
}

let total: number =
    calculateTotal(500, 3);

console.log(total);

30. Function Type Annotations Summary

Function type annotations allow you to clearly define parameter types and return types.

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

This improves type safety, readability, and makes TypeScript functions easier to maintain.

📌 Key Points

  • Function parameters can have type annotations.
  • Return values can also have type annotations.
  • Use : number for numeric return values.
  • Use : string for string return values.
  • Use : boolean for true/false return values.
  • Use : void when a function does not return a value.
  • Multiple parameters can have different types.
  • Union types can be used in function parameters.
  • Function annotations are useful in React and React Native applications.

🧠 Quick Quiz

Question: Which syntax correctly specifies that both parameters are numbers and the function returns a number?