Optional Parameters
When working with functions in TypeScript, you may encounter situations where not all parameters are required for every call. To handle such cases, TypeScript allows you to define optional parameters. An optional parameter is a parameter that does not need to be provided when calling a function. You define an optional parameter by adding a question mark (?) after the parameter name in the function signature. This tells TypeScript that the parameter may be omitted or explicitly passed as undefined.
12345678910function printName(first: string, last?: string): void { if (last) { console.log("Full name:", first + " " + last); } else { console.log("First name:", first); } } printName("Alice", "Smith"); // Output: Full name: Alice Smith printName("Bob"); // Output: First name: Bob
In the printName function above, the last parameter is marked as optional. This means you can call printName with just the first argument, or with both first and last. TypeScript will enforce that any optional parameter must come after all required parameters in the function signature. When you omit an optional parameter, its value inside the function is undefined. This requires you to handle the possibility that the parameter may not be present, as shown by the conditional check in the function body.
Optional parameters provide flexibility in how functions can be called, but they also require you to consider what happens when a value is missing. TypeScript's static checking ensures you do not accidentally skip required parameters, and helps you catch errors related to parameter order and usage. By marking parameters as optional, you can design functions that are both robust and easy to use in a variety of scenarios.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Can you explain what happens if I pass `undefined` explicitly as the optional parameter?
Are there any best practices for using optional parameters in TypeScript?
Can I have multiple optional parameters in a function?
Awesome!
Completion rate improved to 9.09
Optional Parameters
Sveip for å vise menyen
When working with functions in TypeScript, you may encounter situations where not all parameters are required for every call. To handle such cases, TypeScript allows you to define optional parameters. An optional parameter is a parameter that does not need to be provided when calling a function. You define an optional parameter by adding a question mark (?) after the parameter name in the function signature. This tells TypeScript that the parameter may be omitted or explicitly passed as undefined.
12345678910function printName(first: string, last?: string): void { if (last) { console.log("Full name:", first + " " + last); } else { console.log("First name:", first); } } printName("Alice", "Smith"); // Output: Full name: Alice Smith printName("Bob"); // Output: First name: Bob
In the printName function above, the last parameter is marked as optional. This means you can call printName with just the first argument, or with both first and last. TypeScript will enforce that any optional parameter must come after all required parameters in the function signature. When you omit an optional parameter, its value inside the function is undefined. This requires you to handle the possibility that the parameter may not be present, as shown by the conditional check in the function body.
Optional parameters provide flexibility in how functions can be called, but they also require you to consider what happens when a value is missing. TypeScript's static checking ensures you do not accidentally skip required parameters, and helps you catch errors related to parameter order and usage. By marking parameters as optional, you can design functions that are both robust and easy to use in a variety of scenarios.
Takk for tilbakemeldingene dine!