Instruction: Describe how default parameters can be used in TypeScript functions.
Context: This question tests the candidate's knowledge of default parameters in TypeScript functions and their ability to explain how default parameters provide flexibility in function invocations.
Official answer available
Preview the opening of the answer, then unlock the full walkthrough.
For instance, let's consider we have a function that generates a greeting message. In JavaScript, if we want to make sure our function can handle cases where not all arguments are provided, we might need to include additional checks inside the function or use the logical OR operator (||) to assign default values. TypeScript simplifies this by allowing us to directly assign default values to parameters in the function signature itself.
function greet(name: string, greeting: string = "Hello") { return `${greeting}, ${name}!`; }...