Flow: Strange $Diff behaviour

Created on 7 May 2017  路  2Comments  路  Source: facebook/flow

Try flow example

type WithID = {
  id: number,
  title: string,
};

// Next, two options for making a version without an id.
// I think these are equivalent types, but Flow disagrees.
type WithoutIdViaDiff = $Diff<WithID, {id: number}>;
type WithoutIdViaOmission = {title: string};

// Assigning a WithoutIdViaOmission to a WithoutIdViaDiff works.
const x: WithoutIdViaDiff = ({title: ''}: WithoutIdViaOmission);

// But assigning a WithoutIdViaDiff to a WithoutIdViaOmission does not...
const y: WithoutIdViaOmission = ({title: ''}: WithoutIdViaDiff);

Interestingly, the error says

15: const y: WithoutIdViaOmission = ({title: ''}: WithoutIdViaDiff);
                                     ^ WithID. This type is incompatible with
15: const y: WithoutIdViaOmission = ({title: ''}: WithoutIdViaDiff);
             ^ object type

It's interpreting ({title: ''}: WithoutIdViaDiff) as having type WithID even though there's an inline type annotation. Strange.

doesn't repro

All 2 comments

maybe it's a bug? even the example in the docs is failing:

type Props = { name: string, age: number };
type DefaultProps = { age: number };
type RequiredProps = $Diff<Props, DefaultProps>;

function setProps(props: RequiredProps) {
  props.name;
}
6:   props.name;
           ^ property `name`. Property cannot be accessed on
6:   props.name;
     ^ Props

It's a known limitation of $Diff right now. It can only be used as a requirement and not for usage. There is some work being done to improve the situation. Sadly, it's not an easy problem.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ctrlplusb picture ctrlplusb  路  3Comments

davidpelaez picture davidpelaez  路  3Comments

ghost picture ghost  路  3Comments

cubika picture cubika  路  3Comments

funtaps picture funtaps  路  3Comments