TypeScript Optional Parameters in Callbacks Last Updated : 04 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In TypeScript, optional parameters in callbacks are parameters that may or may not be provided when the callback function is invoked. They are denoted by a ? after the parameter name, allowing for more flexible function calls without requiring all arguments.Syntaxtype MyCallback = (param1: string, param2?: number) => void; function processCallback(callback: MyCallback, param1: string, param2?: number) { callback(param1, param2); }Where-MyCallback is a type representing a callback function with a required param1 (string) and an optional 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 with the provided parameters.Example 1: In this example, MyCallback defines an optional parameter num. The callback function logs the string and conditionally logs the number if provided. It's called with and without the number. JavaScript 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."); } }; // Calling the callback with and without the optional parameter myFunction("GeeksforGeeks"); myFunction("A Computer Science Portal", 99); Output:Received: GeeksforGeeks Number is not provided. Received: A Computer Science Portal Number: 99Example 2: In this example, MathOperationCallback defines a callback for math operations on two numbers with an optional description. The performMathOperation function uses this callback, accepting two numbers and an optional description. JavaScript type MathOperationCallback = (a: number, b: number, description?: string) => number; // Function to perform math operation function performMathOperation( operation: MathOperationCallback, a: number, b: number, description?: string ) { const result = operation(a, b, description); console.log(`Operation: ${description || "Unnamed"}`); console.log(`Result: ${result}`); } // Custom addition function const customAddition: MathOperationCallback = (x, y, description) => { const sum = x + y; return sum; }; // Perform operations performMathOperation(customAddition, 5, 3); // No description performMathOperation(customAddition, 10, 7, "Adding Numbers"); // With description Output: Operation: UnnamedResult: 8Operation: Adding NumbersResult: 17 Comment More infoAdvertise with us Next Article TypeScript Generic Parameter Defaults A akshitsaxenaa09 Follow Improve Article Tags : TypeScript Geeks Premier League 2023 Similar Reads TypeScript Optional Parameters Optional parameters in TypeScript allow functions to be called without specifying all arguments, enhancing flexibility and code readability.Denoted by appending a ? to the parameter name.Optional parameters must follow the required parameters in function definitions.Syntaxfunction functionName(param 3 min read Optional Property Class in TypeScript TypeScript is an Object Oriented Programming language that allows you to create classes with optional properties which may or may not be assigned with a value. We will discuss two different ways of creating optional property classes in TypeScript: Table of Content By using the Question Mark(?)By ass 4 min read TypeScript Parameter Type Annotations TypeScript Parameter type annotations are used to specify the expected data types of function parameters. They provide a way to explicitly define the types of values that a function expects as arguments. Parameter type annotations are part of TypeScript's static typing system, and they help catch ty 2 min read Provide the syntax for optional parameters in TypeScript In TypeScript, optional parameters allow you to specify that a function parameter may be omitted when calling the function. You denote optional parameters by adding a question mark (?) after the parameter name in the function declaration. Syntax:function functionName(param1: type, param2?: type, par 2 min read TypeScript Generic Parameter Defaults TypeScript Generic Parameter Defaults allow you to specify default values for generic type parameters. This feature provides a fallback type when no explicit type argument is given, making generic types more flexible and easier to use, particularly when type inference is not straightforward.Syntaxfu 3 min read TypeScript Object Type Optional Properties In TypeScript, optional properties are denoted using the ? modifier after the property name in the object type definition. This means that when an object is created, the optional property can either be provided or omitted.Syntax:type TypeName = { propertyName: PropertyType; optionalPropertyName?: Op 3 min read Like