flow icon indicating copy to clipboard operation
flow copied to clipboard

Width subtyping doesn't work with nested object type structures.

Open charltongroves opened this issue 7 years ago • 3 comments

Flow version: 0.102

Expected behavior

This snipped of code should produce no errors

type NestedSuperType = {
  obj: {
    id: string,
  },
};

type NestedSubType = {
  obj: {
    extraProp: string,
    id: string,
  },
}

const nestedSubObj: NestedSubType = {
  obj: {
    extraProp: "",
    id: '',
  },
};

const nestedSuperObj: NestedSuperType = nestedSubObj;

Link to example in flow.org/try

Actual behavior

If my understanding of width subtyping is correct, the above snipped should return no errors. But instead, it produces this

Cannot assign `nestedSubObj` to `nestedSuperObj` because property `extraProp` is missing in object type [1] but exists in object type [2] in property `obj`

Making all nested objects readonly seems to fix the issue, but i'm fairly certain that shouldn't matter (check out the flow try to see more information)

For example, this works fine without readonly refinement

type SuperType = {
  obj: {
    id: string,
  },
};

type SubType = {
  extraProp: string,
  obj: {
    id: string,
  },
}

const subObj: SubType = {
  extraProp: "",
  obj: {
    id: ""
  }
}

const workingFine: SuperType = subObj;

Link to example in flow.org/try

  • Link to Try-Flow or Github repo: Link

charltongroves avatar Jul 17 '19 04:07 charltongroves

Object properties are invariant by default, meaning subtypes are not going to match, only exact type matches will match. If you make your object properties covariant by wrapping your object types in $ReadOnly for example, then the property types will begin to match subtypes, meaning your errors should go away.

lyleunderwood avatar Jul 17 '19 04:07 lyleunderwood

@lyleunderwood right.

I understand why they're invariant for examples like

type SuperType = {
	obj: {
		[any: string]: ?number
	}
};

type SubType = {
	obj: {
		prop: number,
		[any: string]: ?number,
	}
}

const subObj: SubType = {
	obj: {
		prop: 19
	}
}

const obj: SuperType = subObj;

As flow would then let you do this, which would be unsound

obj["prop"] = null

But I can't see why my example code in the flow.org/try is unsound. As far as I can see, there is no properly typed way you could write to the nested property extraProp.

charltongroves avatar Jul 17 '19 05:07 charltongroves

Update: Turns out my code example was unsound. If the props were covariant, the following code would execute with no issue

const nestedSubObj: NestedSubType = {
  obj: {
    extraProp: "",
    id: '',
  },
};

const nestedSuperObj: NestedSuperType = nestedSubObj;

// No error, despite this also making `nestedSubObj.obj.extraProp` nil
nestedSuperObj.obj = {
	id: "",
}

nestedSuperObj.obj === nestedSubObj.obj // true

nestedSubObj.obj.id === undefined // Invalid with its original type sig

this is obviously very bad.

Good job flow team, glad this isn't a bug.

charltongroves avatar Jul 17 '19 06:07 charltongroves