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 info N nikitamehrotra99 Follow Improve Article Tags : TypeScript Geeks Premier League 2023 Explore TypeScript Tutorial 8 min read TypeScript BasicsIntroduction to TypeScript 3 min read Difference between TypeScript and JavaScript 4 min read How to install TypeScript ? 3 min read Hello World in TypeScript 2 min read How to execute TypeScript file using command line? 2 min read Variables in TypeScript 6 min read What are the different keywords to declare variables in TypeScript ? 4 min read Identifiers and Keywords in TypeScript 2 min read TypeScript primitive typesData types in TypeScript 3 min read TypeScript Numbers 4 min read TypeScript String 4 min read Explain the concept of null and its uses in TypeScript 3 min read TypeScript Object typesWhat are TypeScript Interfaces? 4 min read TypeScript class 4 min read How enums works in TypeScript ? 4 min read TypeScript Tuples 4 min read TypeScript other typesWhat is any type, and when to use it in TypeScript ? 3 min read How to Create an Object in TypeScript? 4 min read What is an unknown type and when to use it in TypeScript ? 3 min read Explain the purpose of never type in TypeScript 3 min read TypeScript combining typesTypeScript Union 3 min read What are type aliases and how to create it in Typescript ? 3 min read TypeScript AssertionsExplain Type assertions in TypeScript 3 min read TypeScript FunctionsHow to write a function in Typescript ? 4 min read How to achieve function overloading in TypeScript ? 2 min read Explain the arrow function syntax in TypeScript 2 min read TypeScript toPrecision() Function 1 min read TypeScript toFixed() Function 2 min read TypeScript toLocaleString() Function 2 min read TypeScript toString() 1 min read TypeScript interfaces and aliasesWhat are TypeScript Interfaces? 4 min read What are type aliases and how to create it in Typescript ? 3 min read TypeScript classesHow to Extend an Interface from a class in TypeScript ? 2 min read How to Create an Object in TypeScript? 4 min read How to use getters/setters in TypeScript ? 5 min read TypeScript Inheritance 3 min read When to use interfaces and when to use classes in TypeScript ? 4 min read Generics Interface in typescript 5 min read How to use property decorators in TypeScript ? 4 min read Like