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.
Syntax:
type MyCallback = (param1: string, param2?: number) => void; function processCallback(callback: MyCallback, param1: string, param2?: number) { callback(param1, param2); }
In there:
- MyCallback is a type that represents a callback function that requires param1 (string) and a param2 (number).
- 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:
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:
Hope this article helps you better understand callback optional parameters in TypeScript.
You should read it
- What are the features of Windows 10 Windows Features?
- Instructions for setting basic parameters on the camera
- Windows 10 now allows users to update drivers for more devices through Windows Updates
- Paint and WordPad will become optional features on Windows 10 20H1
- What is 'Optional Quality Update' on Windows 10?
- Parameters in HTTP
- Instructions for setting up room parameters in Among Us
- Get color parameters more easily with ColorSchemer Studio
May be interested
- Difference between Zsh and Bashboth shells provide powerful scripting languages but they are not fully compatible with each other. let's find out the difference between zsh and bash with tipsmake.com.com!
- How to Install Chromium OStipsmake today will guide you how to install chromium os operating system. chromium os is an open source version of the closed source chrome os operating system that is only available on google's chromebook laptops. chromium os is designed for all pcs, but not all computers are compatible so software problems can still occur. this guide is intended for people who are familiar with the operating system installation process and have advanced computer skills.
- How to Run Java Files (.jar)today's wikihow will show you how to open and run executable jar files on a windows or mac computer. jar files (java archive - java archive) contain data that can be used with java programs. most jar files are simply a medium containing data that another program needs to run java; therefore, you cannot run these files and nothing happens when double-clicking them. similarly, most executable jar files are downloaded as installation files for the purpose of installing applications or programs. therefore, if you have problems opening the file, you should check whether your jar file is compatible with the operating system or not.
- How to Forward URLsthere are many reasons why you might want to forward urls and a few basic ways to approach it. for a website that has many visits and good search results but has to change its domain name, forwarding is the appropriate choice in this transition process. visits to the old domain will automatically forward to the new domain. over time, as search engines update their databases, new domains will be displayed in search results. you perform url forwarding when you need to redirect several other urls to a web address, or you want to shorten a complex url. the url forwarding method depends on your website's programming code and your experience and confidence in editing that code.
- What To Remember When You Start Codingread on to find out the most important things to remember when you start your coding journey.
- How to overclock Raspberry Pi 3although the old raspberry pi 3b and 3b+ models are not as powerful as the new pi 4 or pi 5, you can improve their performance by overclocking the cpu.