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.
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.