Skip to content

Add support for destructuring parameters + default values #183

Closed

Description

@TylerEich

I'm using ES6's new destructuring parameters with default values per the reminder from Brendan Eich (via 2ality.com).

So I would like to do this:

function rgba({ r, g, b, a = 1 }) {
    return [ r, g, b, a ];
}

Flow is throwing an Unexpected token = error at a = 1. Interestingly, JSHint has an issue with this pattern too.

Activity

  1. added
    incompleteness
    and removed on Jan 3, 2015
  2. rauchg commented on Jan 20, 2015

    @rauchg

    +1

  3. cesarandreu commented on Sep 21, 2015

    @cesarandreu
    Contributor

    I have a question related to this issue: What's would be the expected behavior for typing params with default values?

    My usage of default params tends to enforce strict types, e.g. this field is either 1 or some user-defined number, but it is always a number.

    Considering the following:

    /* @flow */
    type params = { a: number }
    function fn ({ a = 1 }: params): params {
      return { a }
    }
    fn({})

    Would I get an error for not passing in an object that has a set to a number? It's optional, so I'm guessing it should probably be annotated as { a?: number } or { a: ?number }?

  4. rauchg commented on Sep 21, 2015

    @rauchg

    It could also infer the type from the default value? Making a?: number unnecessary to write (perhaps more obscure).

  5. samwgoldman commented on Oct 3, 2015

    @samwgoldman
    Member

    @cesarandreu It would be { a?: number }, which is the same as { a: void | number }. ?number is the same as void | null | number. null is not a real type annotation in Flow (edit: it is now), but the point remains.

  6. cesarandreu commented on Oct 3, 2015

    @cesarandreu
    Contributor

    Thanks!

  7. samwgoldman commented on Oct 13, 2015

    @samwgoldman
    Member

    @eyyub You have some work on this. I looked over your branch and I think it makes sense to try to land the parsing support first, then the type checking support separately. If you don't have bandwidth to do this, would you mind if I tried to put a bow on what you have so far?

  8. eyyub commented on Oct 13, 2015

    @eyyub
    Contributor

    @samwgoldman Np I agree ! And yes sorry I don't have 'a bandwith' (nice expression :p) to do this actually, so do as you want and do not hesitate to ask me if something is weird on my branch !

  9. Arilas commented on Dec 22, 2015

    @Arilas

    Any updates for this issue?

  10. AlexGalays commented on Jan 2, 2016

    @AlexGalays

    I need diz !

    Having to refactor

    const { a, b = false } = obj

    into

    let { a, b } = obj;
    if (b === undefined) b = false;
    

    Just to please flow is very annoying :(

  11. trobertsonsf commented on Jan 8, 2016

    @trobertsonsf

    Would love to get a fix for this. Lack of support for such a nice language feature makes it feel like I'm getting handcuffed by flow =/

  12. 31 remaining items

  13. Jacse commented on Jul 14, 2017

    @Jacse

    It's been more than two years since this issue was opened and it still doesn't work properly. What's the state of the issue? @samwgoldman did you improve on the partial fix?

  14. lukemartin commented on Jan 18, 2018

    @lukemartin

    Flow fails with this very simple example of specifying a maybe type with a default null value:

    const foo = ({name = null}:{name: ?string}) => {
    	console.log(name);
    }
    
    1: const foo = ({name = null}:{name: ?string}) => {
                 ^ null or undefined. This type is incompatible with
    1: const foo = ({name = null}:{name: ?string}) => {
    				^ string
    1: const foo = ({name = null}:{name: ?string}) => {
    						^ null. This type is incompatible with
    1: const foo = ({name = null}:{name: ?string}) => {
    				^ string
    

    https://flow.org/try/#0MYewdgzgLgBAZiEMC8MAUBvMBDAtgUxRjAFcAbMgXwC4s99qYB+aAJwEswBzSgShQB8MDACgAkKEggy+AHRkQXNDgK8A3CMpA

  15. Rohit-L commented on May 10, 2018

    @Rohit-L

    Is anybody from the flow team going to comment on this?

  16. simlu commented on Jun 20, 2018

    @simlu

    We are running into the exact problem that @lukemartin describes. Would be great to get this fixed or at least have someone from the team comment on it.

  17. sibelius commented on Jul 18, 2018

    @sibelius

    what is still failing?

  18. simlu commented on Jul 18, 2018

    @simlu

    @sibelius lukemartin has provided a good example. Do you have problems understanding it?

  19. lukemartin commented on Jul 19, 2018

    @lukemartin
  20. Naddiseo commented on Aug 16, 2018

    @Naddiseo

    Using $Subtype around the annotation seems to work, but I'm not sure if that's the right way to go in all situations.

    const foo = ({name = null}: $Subtype<{name: ?string}>) => {
    	console.log(name);
    }
    

    https://flow.org/try/#0MYewdgzgLgBAZiEMC8MAUBvMBDAtgUxRjAFcAbMgXwC4YASAZRICMoBPAB3wB4s99aAfmgAnAJZgA5pQB8AShQyYGAFABIUJBBl8AOjIhJaHATkBuFZRVA

  21. Buggytheclown commented on Oct 19, 2018

    @Buggytheclown
    const foo = ({name = true}: $Subtype<{name: ?string}>) => {
      //                 ^ flow does not check this
      console.log(name);
    }
    

    As proposed in SO

    function foo(arg: {name: ?number}) {
      const { name = 42 } = arg;
      // the default value is only used for undefined values.
      console.log((name: (number | null)));
    }
    
  22. simlu commented on Oct 19, 2018

    @simlu

    @Buggytheclown Not a big fan of changing code to accommodate flow. In our scenario (jetbrains) this would cause us to lose introspection as well.

  23. added a commit that references this issue on Jan 5, 2019
  24. tomscallon commented on Feb 25, 2019

    @tomscallon

    Perhaps a separate issue, but I'm having a very similar problem with non-maybe types. I'm trying:

    /* @flow */
    
    type P = string | Array<string>;
    
    function p(p: P = []) {}
    
    type O = {p: P};
    
    function o({p = []}: O) {}
    

    And I get the following errors:

    9: function o({p = []}: O) {}
                   ^ array type [1] is incompatible with string [1].
    References:
    7: type O = {p: P};
                    ^ [1]
    9: function o({p = []}: O) {}
                       ^ empty array literal [1] is incompatible with string [2].
    References:
    9: function o({p = []}: O) {}
                       ^ [1]
    7: type O = {p: P};
                    ^ [2]
    

    Interestingly, the errors go away if I simplify the type P to Array<string>. It's almost like Flow is expecting my default value to be all of the unioned types, rather than just one of them.

  25. added a commit that references this issue on Jul 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions