xxxxxxxxxx
type Person = {
name?: string,
age?: number,
isActive?: boolean
};
const defaultPerson: Person = {
name: "John Doe",
age: 25,
isActive: true
};
function getPersonDetails(person: Person = defaultPerson) {
// Function logic here
}
// Usage
const person1: Person = {
name: "Alice",
isActive: false
};
getPersonDetails(person1); // Person object with default values for missing properties will be assigned
const person2: Person = {};
getPersonDetails(person2); // Person object with default values for all properties will be assigned
xxxxxxxxxx
function sayName({ first, last = 'Smith' }: {first: string; last?: string }): void {
const name = first + ' ' + last;
console.log(name);
}
sayName({ first: 'Bob' });
xxxxxxxxxx
function greet(name: string = "Guest") {
console.log(`Hello, ${name}!`);
}
// Calling the function without providing an argument for 'name'
greet(); // Output: "Hello, Guest!"
// Calling the function with an argument for 'name'
greet("John"); // Output: "Hello, John!"