I'm new to typescript and now going over the docs and there is this example in "Optional Properties" section :
interface SquareConfig {
color?: string;
width?: number;
}
function createSquare(config: SquareConfig): { color: string; area: number } {
let newSquare = { color: "white", area: 100 };
if (config.color) {
newSquare.color = config.color;
}
if (config.width) {
newSquare.area = config.width * config.width;
}
return newSquare;
}
let mySquare = createSquare({ color: "black" });
Now, i do understand what optional properties are, but what confused me is the : { color: string; area: number } in the function arguments.Is it because they want to do type checking against the variables color and area? and if that is the case why they write them in function arguments rather than place them within the function and do type checking like below
let color:string;
let area: number;
can you please explain what does it do in this code snippet ?