TypeScript optional parameters in Callbacks

TypeScript is a popular programming language used to build powerful and scalable applications. In TypeScript , callback parameters are optional, which means they may or may not be provided when calling the callback.

TypeScript optional parameters in Callbacks Picture 1

Syntax:

type MyCallback = (param1: string, param2?: number) => void; function processCallback(callback: MyCallback, param1: string, param2?: number) { callback(param1, param2); }

In there:

  1. MyCallback is a type that represents a callback function that requires param1 (string) and a param2 (number).
  2. processCallback is a function that accepts a callback of type MyCallBack and optional parameters param1 (string) and param2 (number). It calls the callback function using the provided parameters.

Example 1: Define type MyCallback, where param2 is an optional parameter. The callback function takes two parameters, str (a string) and num (an optional number). Inside this function you record the received string and if num is provided you save that number as well. Otherwise, the number is not provided. The example calls callback myFunction twice: first with just the string parameter, then with both strings and numbers.

type MyCallback = (param1: string, param2?: number) => void; const myFunction: MyCallback = (str, num) => { console.log(`Received: ${str}`); if (num !== undefined) { console.log(`Number: ${num}`); } else { console.log("Number is not provided."); } }; // Gọi callback có và không có tham số tùy chọn myFunction("GeeksforGeeks"); myFunction("A Computer Science Portal", 99); 

Result:

TypeScript optional parameters in Callbacks Picture 2

Example 2: Define the MathOperationCallback type for callback functions that implement mathematical operations on two numbers, with an optional description parameter. The performMathOperation function takes a callback function, two numbers (a and b), and adds an optional description parameter.

type MathOperationCallback = (a: number, b: number, description?: string) => number; function performMathOperation( operation: MathOperationCallback, a: number, b: number, description?: string ) { const result = operation(a, b); console.log(`Operation: ${description || "Unnamed"}`); console.log(`Result: ${result}`); } // Xác định một hàm bổ sung tùy biến const customAddition: MathOperationCallback = (x, y, description) => { const sum = x + y; console.log(`Performing addition: ${description || "Unnamed"}`); return sum; }; // Triển khai các phép tính // kèm hoặc không kèm mô tả // Không cung cấp mô tả performMathOperation(customAddition, 5, 3); // Kèm mô tả performMathOperation(customAddition, 10, 7, "Adding Numbers"); 

Result:

TypeScript optional parameters in Callbacks Picture 3

Hope this article helps you better understand callback optional parameters in TypeScript.

4 ★ | 2 Vote

May be interested

  • Get color parameters more easily with ColorSchemer StudioGet color parameters more easily with ColorSchemer Studio
    in the design world, color is a very important factor that directly affects the quality and aesthetics of the product being created. if you want to know these parameters to be able to use, color schemer studio will be a software worth your attention.
  • Python function parameterPython function parameter
    in the previous article we learned about the built-in python function and the user-defined python function with customizable number of parameters. you will know how to define a function using the default parameters, keyword and custom parameters in this article.
  • Which screen parameters are completely meaningless?Which screen parameters are completely meaningless?
    let's take a closer look at these parameters to see what they mean.
  • How to install and remove optional features in Windows 11How to install and remove optional features in Windows 11
    on windows 11, you can add or remove many optional features to extend the functionality of your computer. here's how to add or remove features on windows 11.
  • stdarg.h in Cstdarg.h in C
    the file header named stdarg.h in library c defines a type of va_list variable and 3 macros that can be used to retrieve parameters in a function when the number of parameters is unknown (eg the number of parameters is can change).
  • Variable parameter in CVariable parameter in C
    sometimes in some cases, when you want a function, you can get parameters instead of predefined parameters. the c language provides you with a solution to this situation and you are allowed to define a function that accepts function parameters based on your requirements.
  • Which game screen parameters do you need to care about?Which game screen parameters do you need to care about?
    your friends tell you that his computer screen has g-sync, 1ms gtg, 16: 9 aspect ratio, hdr screen, you won't see any ghosts on it. do these terms make it difficult for you?
  • How to set up basic parameters for a computerHow to set up basic parameters for a computer
    how to set up basic parameters for a computer
  • The Nox App Player command line parameters supportThe Nox App Player command line parameters support
    this article will list for you some command line parameters that nox app player supports (from version 3.0 and above). these command line parameters are used to set up system settings, launch applications and control nox.
  • Instructions for using CPU-Z, read the CPU-Z parameters providedInstructions for using CPU-Z, read the CPU-Z parameters provided
    cpu-z only works in windows operating system to collect information about some of the main hardware components of the computer.