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

Union types & Default params #2093

Closed
boopathi opened this issue Jul 18, 2016 · 6 comments
Closed

Union types & Default params #2093

boopathi opened this issue Jul 18, 2016 · 6 comments

Comments

@boopathi
Copy link

boopathi commented Jul 18, 2016

Search terms: destructuring, default arguments, default parameters, union

eg:

declare type Options = {
    a: null | Object,
    b: bool | Object,
}

function fn ({
    a = null, // errors here
    b = {} // errors here
} :Options = {}) {
    console.log(a, b);
}

let x :Options = {
    a: null, // No error
    b: {} // No error
};

It's strange that it happens only for default value assignment in object destructuring - fn and not an object definition - x.

This does NOT happen in Flow 0.28. But flow 0.29 throws errors.

@samwgoldman samwgoldman self-assigned this Jul 18, 2016
@echenley
Copy link

echenley commented Mar 17, 2017

@samwgoldman Any update on this? Similar issue in Flow v0.41.0:

// BlockTitle.js
// ...
type Props = {
  children?: React$Element<any>,
  className?: string,
  headerClassName?: string,
  size?: number | string,
  theme: Theme,
}

const BlockTitle = ({
  children,
  className = '',
  headerClassName = '',
  size = 3,
  theme,
}: Props) => {
// ...

And the error:

src/components/common/BlockTitle.js:18
 18:   size = 3,
       ^^^^ number. This type is incompatible with
 18:   size = 3,
       ^^^^ string

src/components/common/BlockTitle.js:18
 18:   size = 3,
       ^^^^ string. This type is incompatible with
 18:   size = 3,
       ^^^^ number

@arahansen
Copy link

I think I'm seeing this same issue. Is there any progress being made on this? or any solid workarounds?

Here's my example I'm running into: tryflow example

@trsh
Copy link

trsh commented Mar 1, 2018

Same here, also with state.

@FireyFly
Copy link
Contributor

FireyFly commented Mar 8, 2019

I'm running into this issue as well, to me it looks like an inconsistency in how Flow handles union types and destructuring with default values:

type Obj = { foo?: string | number };

// Error: "Cannot assign `{...}.foo` to `foo` because string [1] is incompatible with number [2]."
const { foo = 1 }: Obj = {};

// Works!
const obj: Obj = {};
const { foo: bar = 1 } = obj;

// Works!
const baz: $PropertyType<Obj, 'foo'> = 1;

(flow.org/try)

The first example exhibits the problem in question. The second problem is what I feel the first one ought to be equivalent to (unless I'm misunderstanding how the type syntax applies to the left-hand side of the assignment). The third example demonstrates that it isn't a problem with assignments to the union type itself, but only when the assignment happens as a default value when destructuring.

This is also seen in #183 (comment). Possibly more issues as well.

I'm surprised this hasn't been addressed since it seems like a common case that a lot of people run into, and an interaction of fairly basic JS features. Perhaps it's just not a code style that's used much at facebook?

@goodmind
Copy link
Contributor

/cc @samwgoldman was this fixed?

@goodmind
Copy link
Contributor

Fixed in de994b6

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

7 participants