From 6b5c6d22d4286de46b77c5d55c90f58515fcf6ba Mon Sep 17 00:00:00 2001 From: Matthew Stevens Date: Wed, 11 Apr 2018 15:21:56 +0200 Subject: [PATCH] Usage of object spread for exact object types Came across this issue: https://github.com/facebook/flow/issues/2626 and decided it could be in the documentation :) --- website/en/docs/types/objects.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/website/en/docs/types/objects.md b/website/en/docs/types/objects.md index d27e183ff1b..fb3b23db6ae 100644 --- a/website/en/docs/types/objects.md +++ b/website/en/docs/types/objects.md @@ -229,6 +229,19 @@ properties to an exact object type. var foo: {| foo: string |} = { foo: "Hello", bar: "World!" }; // Error! ``` +If you need to combine many exact object types into one, instead of using an intersection type you should use the object type spread: + +```js +type FooT = {| foo: string |} +type BarT = {| bar: number |} + +type FooBarFailT = FooT & BarT +type FooBarT = {| ...FooT, ...BarT |} + +const fooBarFail: FooBarFailT = { foo: '123', bar: 12 } // Error! +const fooBar: FooBarT = { foo: '123', bar: 12 } +``` + ## Objects as maps Newer versions of the JavaScript standard include a `Map` class, but it is