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

Type syntax in destructured arguments of ES6 functions #307

Closed
volkanunsal opened this issue Mar 6, 2015 · 4 comments
Closed

Type syntax in destructured arguments of ES6 functions #307

volkanunsal opened this issue Mar 6, 2015 · 4 comments

Comments

@volkanunsal
Copy link

Say I have a function that looks like this:

var myfunction = ({a: string, b:string})=>{ ... }

When I run this through the transpiler, it complains that I'm using the same argument twice in strict mode –– I assume that's a reference to string. Obviously, the flow syntax is not working here. I am wondering how I should type the destructured arguments to make it work. I suppose I could just remove the strict mode pragma.

@fkling
Copy link

fkling commented Mar 6, 2015

See #235 for a discussion about destructuring.

@jeffmo
Copy link
Contributor

jeffmo commented Mar 24, 2015

As @fkling points out, ({a: string, b: string})=>{...} actually means you wish for the arrow function to take an object with keys a and b and alias them both to a local binding named string. This is a somewhat unfortunate aspect of the JS grammar that we can't really overwrite.

That said, I can't think of a reason why you shouldn't be able to at least annotate the full destructuring pattern:

(it seems Flow currently doesn't parse this)

type funcArg = {a: string, b: string};
var myfunction = ({a, b}:funcArg) => { ... }

@gabelevi Can you think of any reasons why that ^ shouldn't work as a workaround?

@volkanunsal
Copy link
Author

The workaround seems to work fine now.

@adnelson
Copy link

The workaround doesn't allow you to specify default arguments in your parameter declaration :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants