Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object Spread with Exact Flow types

I can't seem to get exact types in flow to work with object spread.

type Point = {| x: number, y: number |};
const p1: Point = { x: 10, y: 10 };
const p2: Point = { ...p1, y: 5 };

Generates an error object literal. Inexact type is incompatible with exact type

This does't produce an error, but modifies p1:

const p3: Point = Object.assign(p1, {y: 5});

Using Object.assign with an empty object also produces the same object literal error:

const p4: Point = Object.assign({}, p1, {y: 5});

If I use type Point = {x: number, y: number}; then object spread works, but ideally I'd like to use an exact type.

like image 461
Andy Avatar asked Mar 23 '17 05:03

Andy


1 Answers

Yeah, this is a known bug. I'm currently working to improve our analysis for object spread to fix this and other issues. The underlying cause is that object spread expressions result in "unsealed" which are incompatible with exact object types. The improved analysis will create sealed objects when possible.

like image 155
Sam Goldman Avatar answered Nov 09 '22 23:11

Sam Goldman