Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exact object type definition and destructuring

I'm defining a State type with an exact shape:

type State = {| someString: string, someNumber: number |};

My initial state satisfies this definition:

const initialState : State = {
  someString: 'hey',
  someNumber: 1,
};

Creating function that takes state and returns a modified copy of it does not, however:

function doStuff (state : State) : State {
  return {
    ...state,
    someNumber: 2,
  };
}

src/flowTest.js:10  10:   return {
              ^ object literal. Inexact type is incompatible with exact type   
9: function doStuff (state : State) : State {
                                        ^^^^^ exact type: object type

Is this flow not understand destructuring or am I missing something? I did notice that changing the type definition to be non-strict (removing | around the brackets) will let this code pass. However I'm trying to make I prevent any typos.

Thanks!

like image 549
Emil Ahlbäck Avatar asked Mar 21 '26 12:03

Emil Ahlbäck


1 Answers

Flow's current support for object spread is a bit lacking still, and Exact object syntax is also relatively new. In this case https://github.com/facebook/flow/issues/2405 appears to be the issue you are hitting.

like image 66
loganfsmyth Avatar answered Mar 24 '26 00:03

loganfsmyth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!