TypeScript Optional Parameters Last Updated : 22 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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(param1: type, param2?: type): returnType { // function body}Parameters:param1: A required parameter of the specified type.param2?: An optional parameter; if omitted, its value is undefined. JavaScript function greet(name: string, greeting?: string): string { if (greeting) { return `${greeting}, ${name}!`; } return `Hello, ${name}!`; } console.log(greet("Alice")); console.log(greet("Bob", "Good morning")); The greet function has two parameters: name (required) and greeting (optional).If greeting is provided, it customizes the message; otherwise, it defaults to "Hello".Greeting Function with Optional Parameter JavaScript function greet(name: string, greeting?: string): string { if (greeting) { return `${greeting}, ${name}!`; } else { return `Hello, ${name}!`; } } console.log(greet("Alice")); console.log(greet("Bob", "Good morning")); The greet function has two parameters: name (required) and greeting (optional).If greeting is provided, it customizes the message; otherwise, it defaults to "Hello".Output:Hello, Alice!Good morning, Bob!Function with Multiple Optional Parameters JavaScript function createUser(username: string, age?: number, email?: string): string { let userInfo = `Username: ${username}`; if (age !== undefined) { userInfo += `, Age: ${age}`; } if (email !== undefined) { userInfo += `, Email: ${email}`; } return userInfo; } console.log(createUser("john_doe")); console.log(createUser("jane_doe", 28)); console.log(createUser("sam_smith", 30, "[email protected]")); The createUser function accepts username (required), age, and email (both optional).It constructs a user information string based on the provided arguments.Output:Username: john_doeUsername: jane_doe, Age: 28Username: sam_smith, Age: 30, Email: [email protected]Best Practices for Using TypeScript Optional ParametersPlace Optional Parameters After Required Ones: Always define optional parameters following all required parameters to maintain clarity and prevent errors. Provide Default Values When Appropriate: Assign default values to optional parameters to ensure predictable behavior when arguments are omitted. Use Union Types for Flexibility: Consider using union types for optional parameters to clearly specify the types that are allowed, enhancing type safety. Comment More infoAdvertise with us Next Article TypeScript Optional Parameters N nikitamehrotra99 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League TypeScript Geeks Premier League 2023 +1 More Similar Reads TypeScript Optional Parameters in Callbacks 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, p 2 min read TypeScript Optional Properties Type TypeScript Opional properties type provides a way of defining the parts that are not necessarily required.TypeScript Optional Properties TypesOptional Properties are properties that are not required mandatorily and can be omitted when not needed.In TypeScript, you can define optional properties in a 6 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 TypeScript Conditional Types In TypeScript, conditional types enable developers to create types that depend on a condition, allowing for more dynamic and flexible type definitions.They follow the syntax T extends U ? X : Y, meaning if type T is assignable to type U, the type resolves to X; otherwise, it resolves to Y.Conditiona 4 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 Like