Typed Optional Config
Write a TypeScript function that creates a personalized greeting with optional configuration using typed parameters. The function takes a name (string) and an optional config object with properties: title?: string, formal?: boolean.
Optional properties in TypeScript use the ? modifier: property?: type. The parameter can be undefined or omitted when calling the function. Use default values for missing properties.
This demonstrates TypeScript's optional parameters and interface-like object typing for configuration patterns commonly used in libraries.
Optional properties in TypeScript use the ? modifier, indicating the property may be present or undefined. This pattern is commonly used for configuration objects where most options have defaults.
TypeScript's structural typing allows passing objects with additional properties, making this pattern flexible. Optional chaining (config?.title) or default values (||) safely access potentially undefined properties.
Optional properties use the ? modifier in TypeScript, indicating the property may be present or undefined. This pattern is common for configuration objects where most options have sensible defaults. TypeScript's structural typing allows passing objects with additional properties beyond the defined type.
Example Input & Output
Algorithm Flow

Solution Approach
Define the config parameter type inline: config?: { title?: string; formal?: boolean }. Access config properties with optional chaining or default values using ||.
If formal is true, return 'Good day, Title Name'. If false, return 'Hi Name!'. Use default title 'Mr.' if not provided.
Best Answers
function greetWithConfig(name: string, config?: { title?: string; formal?: boolean }): string {
var title = (config && config.title) || 'Mr.';
if (config && config.formal) return 'Good day, ' + title + ' ' + name + '!';
if (config && config.title) return 'Hi ' + title + ' ' + name + '!';
return 'Hi ' + name + '!';
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
