When working with JavaScript, I often find myself writing functions like the one below to utilize ES6's property shorthand feature:
function exampleFunction({param1, param2}) {
console.log(param1 + " " + param2);
}
//Usage:
const param1 = "param1";
const param2 = "param2";
exampleFunction({param1, param2});
Now, when attempting to write this function in TypeScript, I encounter a slight inconvenience:
function exampleFunction(arg: {param1: string, param2: string}) : string {
return arg.param1 + " " + arg.param2;
}
Although it works fine, I find myself having to prefix the arguments with "arg" and provide a redundant name ("arg") every time I define the function. Is there a way in TypeScript to avoid this by directly using the argument names without specifying the interface each time? I envision something like this:
function exampleFunction({param1: string, param2: string}) : string {
return param1 + " " + param2;
}
A cleaner and simpler approach that still offers the advantages of TypeScript. Is there a solution to achieve this?