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 Optional Property Class in TypeScript N nikitamehrotra99 Follow Improve Article Tags : TypeScript Geeks Premier League 2023 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 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 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 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 Like