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

Spread on union type fails with "Could not decide which case to select" #6342

Closed
almost opened this issue May 22, 2018 · 8 comments
Closed

Comments

@almost
Copy link

almost commented May 22, 2018

This code:

Try it with Try Flow here

type Action1 = {
  name: 'A1',
}

type Action2 = {
  name: 'A2',
}

type Action = 
  | Action1
  | Action2;

function foo( body:Action):Action {
  return {...body};
}

Produces this error:

    14:   return {...body};
                 ^ Could not decide which case to select. Since case 1 [1] may work but if it doesn't case 2 [2] looks promising too. To fix add a type annotation to .name [3].
        References:
        10:   | Action1
                ^ [1]
        11:   | Action2;
                ^ [2]
        14:   return {...body};
                     ^ [3]

Replacing return {...body}; with return body fixes it. It doesn't seem like flow should have to decide on a member of the union just to use the spread operator?

@szist
Copy link

szist commented Jun 13, 2018

Any progress on this? Is there a workaround annotation we could use in the meantime?

@mrkev
Copy link
Contributor

mrkev commented Jun 19, 2018

Spreads on objects are have a few issues at the moment and are being reworked (hopefully soon)

@jcready
Copy link
Contributor

jcready commented Jun 19, 2018

This doesn't really have to do with spread, you get the same error when you do the follow (which is essentially doing the same thing as spread):

function foo( body:Action):Action {
  return { name: body.name };
}

@mrkev
Copy link
Contributor

mrkev commented Jun 20, 2018

Oh true, this is a separate bug.

@frontendphil
Copy link

Is there anything we can do to work around this atm? I'm also running into this issue and it is quite frustrating.

@villesau
Copy link
Contributor

this will fix the issue: #7298

@pirate-matt
Copy link

This hurts me a bit... but seems to be a workaround:

type Action1 = {
  name: 'A1',
};

type Action2 = {
  name: 'A2',
};

type Action = Action1 | Action2;

function foo(body: Action): Action {
  const flowIsBroken: any = { ...body };
  const newlyTypedBody: Action = flowIsBroken;
  return newlyTypedBody;
}

const shouldBeActionType = foo({ name: 'A1' });

// no errors means it's been typed correctly?
foo(shouldBeActionType);

@goodmind goodmind added this to the New Object Spread semantics milestone Jul 17, 2019
@nmote
Copy link
Contributor

nmote commented Oct 25, 2019

This no longer errors in master

@nmote nmote closed this as completed Oct 25, 2019
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

9 participants