Lesson 27 of 60 – Function Types
45%

Function Types in TypeScript

In TypeScript, a function type describes the parameters a function accepts and the value it returns. Function types help TypeScript check that functions are used correctly.

Note: Function types are especially useful when storing functions in variables, passing functions as parameters, and working with callbacks in React and React Native.

1. What is a Function Type?

A function type describes the structure of a function. It specifies its parameter types and return type.

(a: number, b: number) => number

This means the function accepts two numbers and returns a number.

2. Basic Function Type Syntax

A function type can be written using parameter types followed by the return type.

let operation: (
    a: number,
    b: number
) => number;

The variable can store a function matching this structure.

3. Assigning a Function to a Function Type

A function can be assigned to a variable with a function type.

let add: (
    a: number,
    b: number
) => number;

add = (
    a: number,
    b: number
): number => {

    return a + b;
};

4. Function Type with Two Numbers

Here the function must accept two numbers and return a number.

let calculate: (
    x: number,
    y: number
) => number;

calculate = (
    x,
    y
) => x + y;

console.log(
    calculate(10, 20)
);

Output:

30

5. Function Type with String

A function type can specify string parameters and a string return value.

let greet: (
    name: string
) => string;

greet = (
    name
) => "Hello " + name;

console.log(
    greet("Amit")
);

6. Function Type Returning Boolean

A function type can return a boolean value.

let checkAge: (
    age: number
) => boolean;

checkAge = (
    age
) => age >= 18;

console.log(
    checkAge(20)
);

7. Function Type with void

Use void when the function does not return a value.

let showMessage: (
    message: string
) => void;

showMessage = (
    message
) => {

    console.log(message);
};

8. Function Type with Multiple Parameters

Function types can contain several parameters.

let studentInfo: (
    name: string,
    age: number,
    active: boolean
) => string;

Any assigned function must follow this structure.

9. Assigning a Compatible Function

The assigned function must be compatible with the declared function type.

let studentInfo: (
    name: string,
    age: number
) => string;

studentInfo = (
    name,
    age
) => {

    return name + " is " + age;
};

10. Function Type and Type Checking

TypeScript checks whether the assigned function follows the declared function type.

let add: (
    a: number,
    b: number
) => number;

add = (
    a,
    b
) => a + b;

This is valid because the parameter and return types match.

11. Function Type with Optional Parameter

Function types can also contain optional parameters.

let greet: (
    name?: string
) => string;

greet = (
    name
) => {

    return name
        ? "Hello " + name
        : "Hello Guest";
};

12. Function Type with Default Parameter

The function assigned to a function type can use a default parameter.

let greet: (
    name?: string
) => string;

greet = (
    name = "Guest"
) => {

    return "Hello " + name;
};

13. Function Type with Array Parameter

Function types can accept arrays.

let total: (
    numbers: number[]
) => number;

total = (
    numbers
) => {

    return numbers.reduce(
        (sum, number) =>
            sum + number,
        0
    );
};

console.log(
    total([10, 20, 30])
);

14. Function Type with Object Parameter

A function type can also accept an object.

type Student = {
    name: string;
    age: number;
};

let showStudent: (
    student: Student
) => string;

showStudent = (
    student
) => {

    return student.name;
};

15. Function Type Using a Type Alias

A type alias can make a function type easier to reuse.

type AddFunction = (
    a: number,
    b: number
) => number;

let add: AddFunction;

add = (
    a,
    b
) => a + b;

16. Reusing a Function Type

Once a function type is created, it can be used by multiple variables.

type Operation = (
    a: number,
    b: number
) => number;

let add: Operation;
let multiply: Operation;

add = (
    a,
    b
) => a + b;

multiply = (
    a,
    b
) => a * b;

17. Function Type for Subtraction

The same function type can be used for different operations.

type Operation = (
    a: number,
    b: number
) => number;

const subtract: Operation = (
    a,
    b
) => a - b;

console.log(
    subtract(30, 10)
);

Output:

20

18. Function Type for Multiplication

Here is another function using the same function type.

type Operation = (
    a: number,
    b: number
) => number;

const multiply: Operation = (
    a,
    b
) => a * b;

console.log(
    multiply(5, 4)
);

19. Passing a Function as an Argument

A function type can be used when another function receives a function as a parameter.

function calculate(
    a: number,
    b: number,
    operation: (
        x: number,
        y: number
    ) => number
): number {

    return operation(a, b);
}

20. Passing an Arrow Function

An arrow function can be passed to a function expecting a function type.

function calculate(
    a: number,
    b: number,
    operation: (
        x: number,
        y: number
    ) => number
): number {

    return operation(a, b);
}

const result = calculate(
    10,
    20,
    (x, y) => x + y
);

console.log(result);

21. Function Type for Callback

Function types are commonly used for callback functions.

function processNumber(
    number: number,
    callback: (
        value: number
    ) => number
): number {

    return callback(number);
}

console.log(
    processNumber(
        10,
        value => value * 2
    )
);

22. Function Type with void Callback

A callback can return no value.

function showMessage(
    message: string,
    callback: (
        value: string
    ) => void
): void {

    callback(message);
}

showMessage(
    "Hello",
    message => {
        console.log(message);
    }
);

23. Function Type in React

Function types are useful for typing event handlers and callback props in React.

type ClickHandler = () => void;

const handleClick: ClickHandler = () => {

    console.log("Button clicked");

};

This ensures the handler does not require parameters and does not return a value.

24. Function Type in React Native

React Native applications also use function types for button and component event handlers.

type PressHandler = () => void;

const handlePress: PressHandler = () => {

    console.log(
        "Button pressed"
    );

};

25. Function Type for a Value Handler

A function type can accept a value and return nothing.

type ValueHandler = (
    value: string
) => void;

const handleValue: ValueHandler = (
    value
) => {

    console.log(value);

};

This pattern is useful for reusable components.

26. Function Type with Rest Parameters

A function type can also describe a function that accepts a variable number of arguments.

type AddFunction = (
    ...numbers: number[]
) => number;

const add: AddFunction = (
    ...numbers
) => {

    return numbers.reduce(
        (sum, number) =>
            sum + number,
        0
    );
};

console.log(
    add(10, 20, 30)
);

27. Common Function Type Mistake

The function's parameters and return value should be compatible with the declared function type.

type AddFunction = (
    a: number,
    b: number
) => number;

const add: AddFunction = (
    a,
    b
) => {

    return a + b;
};

A function that returns an incompatible type should not be assigned to this function type.

28. When to Use Function Types

Function types are useful when:

  • Storing functions in variables.
  • Creating reusable function definitions.
  • Passing functions as parameters.
  • Creating callback functions.
  • Typing React event handlers.
  • Typing React Native component callbacks.

29. Complete Practical Example

The following example creates a reusable calculator using a function type.

type Operation = (
    a: number,
    b: number
) => number;

const add: Operation = (
    a,
    b
) => a + b;

const multiply: Operation = (
    a,
    b
) => a * b;

function calculate(
    a: number,
    b: number,
    operation: Operation
): number {

    return operation(a, b);
}

console.log(
    calculate(10, 20, add)
);

console.log(
    calculate(10, 20, multiply)
);

Output:

30
200

30. Function Types Summary

Function types describe the structure of functions in TypeScript.

type Operation = (
    a: number,
    b: number
) => number;

They make code safer and easier to understand, especially when functions are passed around as values or callbacks.

📌 Key Points

  • A function type describes parameters and a return type.
  • Function types use the => syntax.
  • Function types can be assigned to variables.
  • They can be created using type aliases.
  • They are useful for callback functions.
  • Function parameters can be strings, numbers, arrays, objects, and other types.
  • Return types can be number, string, boolean, void, and more.
  • Function types can describe rest and optional parameters.
  • They are commonly used with React and React Native.
  • Function types improve type safety and code readability.

🧠 Quick Quiz

Question: What does a TypeScript function type describe?