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

Flow error only when importing a constant from another file #6879

Open
tonai opened this Issue Sep 11, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@tonai

tonai commented Sep 11, 2018

This is a strange bug that only happen when importing a constant from another file.

The following code is ok and do not generate any error:

/* @flow */

type StackItem =
  | {
      type: 'TYPE_1'
    }
  | {
      trajectory: number,
      type: 'TYPE_2'
    };

const TYPE_2 = 'TYPE_2';

function test(item: StackItem) {
  switch (item.type) {
      case TYPE_2:
        console.log(item.trajectory);
        break;
    }
}

Tested online : https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgDKGAhgMYDWAkhngLZgC8qYYAPmAN4uu-b4AuMAHIAKgE0ACgFEA+gEZhPVgF8eHbrz4AnEgCs8ZDHG1YhAOwCudAEZ5tAGmV9ceIWKlyATEq0qA3OhkcOYAzhhgEjKyXkwiUd7CgahQluZGAJYhYLThABQZtHRCxOTURQCUXDyhCIVkABZgBUUAdPx4VZpaYGQkoQQJMQLOWsFhcDB4rfAA5i307boGRiZYFYE9vDbaeCQUm7xqakA

But when you try to import TYPE_2 from another file, it breaks.

b.js:

export const TYPE_2 = 'TYPE_2';

a.js:

/* @flow */

import { TYPE_2 } from './b';

type StackItem =
  | {
      type: 'TYPE_1'
    }
  | {
      trajectory: number,
      type: 'TYPE_2'
    };

function test(item: StackItem) {
  switch (item.type) {
    case TYPE_2:
      console.log(item.trajectory);
      break;
  }
}

This is the error I got:

Error ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ src/a.js:17:24

Cannot get item.trajectory because property trajectory is missing in object type [1].

 [1] 14│ function test(item: StackItem) {
     15│   switch (item.type) {
     16│     case TYPE_2:
     17│       console.log(item.trajectory);
     18│       break;
     19│   }
     20│ }
@wchargin

This comment has been minimized.

Show comment
Hide comment
@wchargin

wchargin Sep 11, 2018

Contributor

Adding // @flow to the top of b.js fixes the problem.

Contributor

wchargin commented Sep 11, 2018

Adding // @flow to the top of b.js fixes the problem.

@tonai

This comment has been minimized.

Show comment
Hide comment
@tonai

tonai Sep 13, 2018

@wchargin Thanks for your reply.

It is indeed the reason why this does not work.
But in fact the b.js file comes from a compiled modules located in node_modules...
So I can't modify it :/

How can I get around that problem ?

tonai commented Sep 13, 2018

@wchargin Thanks for your reply.

It is indeed the reason why this does not work.
But in fact the b.js file comes from a compiled modules located in node_modules...
So I can't modify it :/

How can I get around that problem ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment