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 operator breaks bounded polymorphism #3061

Closed
Mr-Wallet opened this issue Dec 21, 2016 · 4 comments
Closed

Spread operator breaks bounded polymorphism #3061

Mr-Wallet opened this issue Dec 21, 2016 · 4 comments

Comments

@Mr-Wallet
Copy link

/* @flow */
type Un = A | B
type A = { x: string };
type B = { y: string };

function myFunc<T: Un>(foo: T): T {
  const bar = { ...foo };
  return bar;
}

I would expect flow to say, "Well, T is a plain object, and you spread T into an object and returned it, so it must be a T. All good!"

Instead I get object literal. Could not decide which case to select.

@erikdesjardins
Copy link

erikdesjardins commented Dec 22, 2016

To be pedantic, T is an arbitrary subtype of Un, which includes classes that have an x: string or y: string property (try flow), so the provided code shouldn't typecheck.

However, there is still a problem here, because it also happens without generics (try flow).

@Mr-Wallet
Copy link
Author

This is indeed just a special case of "the spread operator doesn't work so well right now", but I felt it bore mention just to make sure it didn't get missed.

@villesau
Copy link
Contributor

This will fix the issue: #7298

@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

5 participants