Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lambda syntax different between returning primitive and returning object #11964

Closed
Jameskmonger opened this issue Oct 31, 2016 · 1 comment
Closed
Labels
Question An issue which isn't directly actionable in code

Comments

@Jameskmonger
Copy link

TypeScript Version: 2.0.3 / nightly (2.1.0-dev.201xxxxx)

Code

// A *self-contained* demonstration of the problem follows...
const fn_prim = () => 3;
const fn_obj = () => {
    val: 3
};

Expected behavior:
I expect it to compile fine. fn_prim should return 3 when called, fn_obj should return { val: 3 } when called.

Actual behavior:
"Unused label" error on val:

@vladima
Copy link
Contributor

vladima commented Oct 31, 2016

TypeScript follows ES spec and per spec your second example is an arrow function where body is a function body wrapped in { } so val: 3 is interpreted as labelled statement with label val and expression statement with expression 3. In order to make it an arrow function that returns an object literal you'll need to wrap it in parens:

const fn_obj = () => ({
    val: 3
});

Related link: MDN: Arrow functions, section Returning object literals

@vladima vladima added the Question An issue which isn't directly actionable in code label Oct 31, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

2 participants