Flow: Regression: Can't use default value in parameter object destructuring when type is union

Created on 5 Aug 2016  Â·  8Comments  Â·  Source: facebook/flow

This broke in Flow v0.29.0. It worked in previous versions.

/* @flow */
function f({a=5}: {a: number|Function}) {
}
$ ./flow29/flow check
test.js:2
  2: function f({a=5}: {a: number|Function}) {
                 ^ function type. This type is incompatible with
  2: function f({a=5}: {a: number|Function}) {
                 ^ number

test.js:2
  2: function f({a=5}: {a: number|Function}) {
                 ^ number. This type is incompatible with
  2: function f({a=5}: {a: number|Function}) {
                 ^ function type


Found 2 errors
bug

Most helpful comment

@samwgoldman Any update on this?

All 8 comments

@samwgoldman This might be because of BecomeT pinning down things after splitting the union?

Confirmed. I'm on this.

@samwgoldman Any update on this?

We've hit this problem today. We've encountered several open issues on this (such as #183, in the last few comments).

Any way to circumvent that problem?

It's not pretty, but you can fall back to what babel compiles to:

const Icon = ({ size }: IconProps) => {
  size = size === undefined ? "sm" : size;

@threehams or more es6 way — props: IconProps with later destructuring: const { size = "sm" } = props

Any updates on this ?

Seems like this is still a problem.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mjj2000 picture mjj2000  Â·  3Comments

cubika picture cubika  Â·  3Comments

pelotom picture pelotom  Â·  3Comments

Beingbook picture Beingbook  Â·  3Comments

marcelbeumer picture marcelbeumer  Â·  3Comments