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

Disjoint unions not working with imported constants #4173

Open
JavoByte opened this issue Jun 14, 2017 · 3 comments
Open

Disjoint unions not working with imported constants #4173

JavoByte opened this issue Jun 14, 2017 · 3 comments

Comments

@JavoByte
Copy link

Hi, I'm trying to use disjoint unions to check a redux reducer. I have a constants file where I export all my constants, like this:

export const USERS_LOAD = "USERS_LOAD";
export const USERS_LOADED = "USERS_LOADED";

The problem is, if I import this constants and define my type Action like this:

type Action =
  | { type: typeof USERS_LOADED, users: Array<{}>}
  | { type: typeof API_ERROR, error: ?any}
  | { type: typeof USERS_LOAD };

And use it un my code like this:

function users(state: State = {
  loading: false,
  all: [],
}, action: Action): State {
  switch (action.type) {
    case USERS_LOAD :
      return {
        ...state,
        loading: true,
      };
    case USERS_LOADED :
      return {
        loading: false,
        all: action.users.map(user => new User(user)),
        error: null,
      };
    default :
      return state;
  }
}

I get the error in USERS_LOADED case: Property not found in action

However, if I define the constants directly in the file instead of importing them, the error goes away and works like expected

@agentcooper
Copy link
Contributor

typeof USERS_LOAD is string and not literal 'USERS_LOAD': example. See also: #2639.

@JavoByte
Copy link
Author

You're right. I changed that because without typeof I get the error

Ineligible value used in/as type annotation (did you forget 'typeof'?)

So, the real question is? Can I use the disjoint types with constants like I said, or it must be using literals?

@0rvar
Copy link

0rvar commented Jun 15, 2017

#4175

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants