Flow: Spreading $Shape of exact type is same as spreading of same exact type

Created on 19 Sep 2018  路  4Comments  路  Source: facebook/flow

It seems when spreading a $Shape of an exact type, it doesn't actually spread on type $Shape<T> but just spreads on T instead. The following breaks in try flow

type A = {| foo: number |};
type B = {| bar: string |};
type C<T, U> = {|
   ...T,
   ...$Shape<U>
|};

const c: C<A, B> = { // Error, property `bar` missing
  foo: 1
};
destructors spread

Most helpful comment

Ok, I've been using the following instead actually:

type Partial<T> = $Rest<T, {}>;

https://flow.org/try/#0C4TwDgpgBACghgJ2ASzgGwDwBUB8UC8UAJAEoQDOw2ANFAN4C+OA3AFCuiRQCCB9APlABmAexEAuKADsArgFsARhARR+DNp2gAhPnUELEkygmRSA5qvUdw0AMI0oAVTyE9rKB4B03rNXdfveCRUTGdWNTZWAGMRKUooKMl7blotF3p-UQkoAEY-dSA

All 4 comments

Any updates on this issue? Is this a bug then in flow or are we misusing the spread operator with the $Shape type because we don' understand some behavior going on here?

Shape doesn't behave quite as it states in the docs. To make all the keys optional, you can do something like this:

type Partial<T> = $Exact<{...{...T}}>;

Here's a link to Try Flow: https://flow.org/try/#0C4TwDgpgBACghgJ2ASzgGwDwBUB8UC8UAJAKIAecAxsBgN4B0jDjWAvqzgNwBQ3okUAIIEotAD5QAZgHtpALigA7AK4BbAEYQEUMax79oAIRHio6xAoDOwBMkUBzHXr7hoAYWwAaKAFU8hcW4oYMZ6LE8gkMZ4JFRMP25dHm5KaUVrKEoFD0FvQ39RSJl5KABGCODzAC8FABZuPSA

Ok, I've been using the following instead actually:

type Partial<T> = $Rest<T, {}>;

https://flow.org/try/#0C4TwDgpgBACghgJ2ASzgGwDwBUB8UC8UAJAEoQDOw2ANFAN4C+OA3AFCuiRQCCB9APlABmAexEAuKADsArgFsARhARR+DNp2gAhPnUELEkygmRSA5qvUdw0AMI0oAVTyE9rKB4B03rNXdfveCRUTGdWNTZWAGMRKUooKMl7blotF3p-UQkoAEY-dSA

Would it make sense to define $Shape's as $Rest<T, {}>? Are there things that rely on $Shape's behavior that wouldn't work this change?

Was this page helpful?
0 / 5 - 0 ratings