Flow: Errors when using destructuring and union types

Created on 9 Aug 2016  Ā·  9Comments  Ā·  Source: facebook/flow

Hello !

First thank you for your awesome work on this project ! šŸ‘

On our project we encountered a bug with flow's handling of destructuring. It seems like it does not deduce the attributes available from the property on which destructuring is applied. This bug can be circumvented by using a generic clone function which does the destructuring :

function clone<T>(obj: T) : T {
    return {...obj,};
}

There is quiet the same issue when creating an object from a $Shape of its type and filling the object with default values for missing properties. Using an Object instead of a Shape makes the error disappear.

I pushed an example here : https://github.com/abalhier/flowPlayground/blob/master/destructuring.js
Or you can see errors directly here on flow website.

It seems to happen in flow 0.27+. I didn't try with older versions.

unionintersections bug

Most helpful comment

Hi all,
we're still encountering the problem.
Any news on this?

All 9 comments

There seems to be an unfortunate interaction between the new union type algorithms and spreads. It's not clear where to annotate to clear the ambiguity. Small repro:

type Foo = { type: 'A'; } | { type: 'B'; };

function copy(a: Foo) : Foo {
    return {...a};
}

@avikchaudhuri - mind taking a look?

@gabelevi Sure, it's on my queue already! (This problem was noted in the new union type implementation...currently spreads have orthogonal bugs in their implementation that interact especially badly with unions).

Hi all,
we're still encountering the problem.
Any news on this?

Still happening on master branch

Is this on a roadmap? It would be super nice to use destructured action unions in Redux reducers:

export type Action = Update | Remove;
export default function reducer(state: State = initialState, action: Action) {
  const { type, payload } = action;
  switch (type) {
    case 'UPDATE':
      return {
        ...state,
        [payload.field]: payload.value,
      };
    case 'REMOVE':
      return {
        ...state,
        array: state.array.filter(item => item.code !== payload),
      };
  }
}

In your example if the union type doesn't share the same structure it would
cause an error, I wouldn't type my actions like that

On Aug 28, 2017 4:41 AM, "Michał Pierzchała" notifications@github.com
wrote:

Is this on a roadmap? It would be super nice to use destructured action
unions in Redux reducers:

export type Action = Update | Remove;export default function reducer(state: State = initialState, action: Action) {
const { type, payload } = action;
switch (type) {
case 'UPDATE':
return {
...state,
[payload.field]: payload.value,
};
case 'REMOVE':
return {
...state,
array: state.array.filter(item => item.code !== payload),
};
}
}

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/facebook/flow/issues/2220#issuecomment-325309033, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ADwSBTN39feRNnYLqFbQJocgrhMnW9a1ks5scossgaJpZM4JgRyY
.

@eloytoro thanks, but I would like to do it, like the docs say: https://flow.org/en/docs/react/redux/#toc-typing-redux-reducers

you're not following

if your actions dont have the same structure you shouldnt use union types
for them, otherwise deconstructuring them is unsafe.

https://flow.org/try/#0C4TwDgpgBAysCGxoF4oG8oGoBu8A2ArhAFxQDyARgFYQDGwUAvgNwBQrokUAYgPa8BBegEteAOyioMnElABE3MmTkAaKGHgg8veABNSlGvSZsZUAELwATkOCiJUqDNJzzAgEqr1m7XoPU6BhZ2M1t7SR5+MPEoAB8La2ixNlYAMwIxERirCF0CWggrAAoAZwQkUjhECDV4LLFSJIBKSvLoNFYoKFpxMvQncBrvLR1dJgi6u3E2LpKAd2FgWgALKCKZJvROru74EugFJTlSHOACKwkMADobsuq1XEJZSfsrjRG9E22u2j2Dt08JwgZwu-RuVzuSAe+CIpBe4jePlGXx2UF0EFS8AIeGAxG+O1O5wkkIgMyYrEYQA

On Mon, Aug 28, 2017 at 8:31 AM Michał Pierzchała notifications@github.com
wrote:

@eloytoro https://github.com/eloytoro thanks, but I would like to do
it, like the docs say:
https://flow.org/en/docs/react/redux/#toc-typing-redux-reducers

—
You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
https://github.com/facebook/flow/issues/2220#issuecomment-325353803, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ADwSBZqhI3uGUB_zyWYMfDKEKAOB26dzks5scsEfgaJpZM4JgRyY
.

Was this page helpful?
0 / 5 - 0 ratings