Skip to content

Commit

Permalink
Usage of object spread for exact object types
Browse files Browse the repository at this point in the history
Came across this issue: facebook#2626 and decided it could be in the documentation :)
  • Loading branch information
mattgstevens committed Apr 11, 2018
1 parent 7464b18 commit eddd3f0
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions website/en/docs/types/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a class="toc" id="toc-objects-as-maps" href="#toc-objects-as-maps"></a>

Newer versions of the JavaScript standard include a `Map` class, but it is
Expand Down

0 comments on commit eddd3f0

Please sign in to comment.