Skip to content

Flow Union Types do not work with RecordOf #1882

Open
@Dagur

Description

@Dagur

What happened

Cannot do any refinement to find out which type I'm working with and silence flow

How to reproduce

// @flow

import {
  type RecordOf,
} from "immutable";

type T1Props = {
  type: "t1",
  stuff: string
};
type T2Props = {
  type: "t2",
  stuff: string,
  moreStuff: string
};
type T1 = RecordOf<T1Props>;
type T2 = RecordOf<T2Props>;

function someFunc(test: T1 | T2) {
  if (test.type === "t2") {
    // Cannot get `test.moreStuff` because: Either property `moreStuff` is missing in  `RecordInstance` [1]. Or property `moreStuff` is missing in  `T1Props` [2]
    console.log(test.moreStuff);
  }

  // all branches are incompatible: Either property `moreStuff` is missing in  `RecordInstance` [1]. Or property `moreStuff` is missing in  `T1Props` [2].
  if (test.moreStuff) {
    console.log("We have t2");
  }
}

I was able to use a simple if to see if a property exists but now that I'm trying to upgrade to 4.0.0 I cannot find a way that works.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions