3 ms·
Whenever I come back to writing JavaScript after a long break, I always forget how to correctly return objects from lambda expressions, and end up with this:
by benrbray 5y ago
Whenever I come back to writing JavaScript after a long break, I always forget how to correctly return objects from lambda expressions, and end up with this:
() => {
foo: "bar";
baz: "qux";
}
Thankfully TypeScript warns me it's a ()=>void function instead of whatever was expected in the context. I scratch my head for a while and then remember:
() => ({
foo: "bar",
baz: "qux"
});
- Rompect 5y agoI'm more baffled that foo: "bar"; is a valid expression. I know, it's the named statement syntax normally used for loops, but I didn't know this worked for any expression.
- rudian 5y agoIn JavaScript statements don’t have to do anything. "hi"; 1; true; new Date(); () => {}; This is valid JavaScript