Reputation: 7064
What is the meaning of an object param in a function signature? I came across this code snippet, and I'm not sure what's going on here.
export const items = (state: any = [], {type, payload}) => {
case (type) {
...
}
};
I don't understand the {type, payload} in the function signature.
Upvotes: 1
Views: 55
Reputation: 4829
This is an example of destructuring.
You can see what this:
let items = (state: any = [], {type, payload}) => {
};
compiles to on the TypeScript playground:
var items = function (state, _a) {
if (state === void 0) { state = []; }
var type = _a.type, payload = _a.payload;
};
And from that I can infer that it means that the second parameter of the function will be an object with a property called "type", and another property called "payload". Further, I will be able to refer to "type" and "payload" directly in my function body:
let items = (state: any = [], {type, payload}) => {
console.log(type);
console.log(payload);
};
let myobj = {
payload: "blue",
type: "no-type"
}
items(null, myobj);
Upvotes: 3