Skip to content

Commit 848af47

Browse files
authored
Merge pull request #69 from jnv/master
docs(README.md): update info about type spread operator in TS
2 parents 8e3053d + 64e0c28 commit 848af47

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Some of these differences are subjective (e.g. error readability), and I'd love
2727
| specifying generic parameters for type definitions | yes | yes |
2828
| typings for public libraries | plenty of well maintained typings | a handful of mostly incomplete typings |
2929
| unique features | <ul><li>autocomplete for object construction</li><li>declarable `this` in functions (typing `someFunction.bind()`)</li><li>large library of typings</li><li>more flexible [type mapping via iteration](https://github.com/Microsoft/TypeScript/pull/12114)</li><li>namespacing</li></ul> | <ul><li>variance</li><li>existential types `*` (deprecated since 0.72)</li><li>testing potential code-paths when types not declared for maximum inference</li><li>`$Diff<A, B>` type</li></ul> |
30-
| type spread operator | [shipped](https://github.com/Microsoft/TypeScript/pull/28234) > 3.2rc | [shipped](https://github.com/facebook/flow/commit/ad443dc92879ae21705d4c61b942ba2f8ad61e4d) >=0.42 |
30+
| type spread operator | no ([planned](https://github.com/microsoft/TypeScript/issues/10727)) | [shipped](https://github.com/facebook/flow/commit/ad443dc92879ae21705d4c61b942ba2f8ad61e4d) >=0.42 |
3131
| support for nullish coalescing proposal | [shipped](https://github.com/microsoft/TypeScript/pull/32883) > 3.7beta | yes |
3232
| support for decorators proposal | yes, legacy proposal | only parsing of legacy proposal, no type-checking |
3333
| support for extending built-in types | yes | no |
@@ -1069,6 +1069,24 @@ function incAge(age: Age): number {
10691069
}
10701070
```
10711071

1072+
## Object type spread
1073+
1074+
Object type spread acts as [object spread](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) but for types. Unlike [intersection types](https://flow.org/en/docs/types/intersections/) type spreads work with exact object types and overwrite existing properties.
1075+
1076+
```js
1077+
type Foo = {| foo: string, bar: string |}
1078+
type Bar = {| bar: number |}
1079+
1080+
type FooBarIntersection = Foo & Bar
1081+
type FooBarSpread = {| ...Foo, ...Bar |}
1082+
1083+
const fooBarInterect: FooBarIntersection = { foo: '123', bar: 12 } // not ok
1084+
const fooBarString: FooBarSpread = { foo: '123', bar: 'string' } // not ok
1085+
const fooBar: FooBarSpread = { foo: '123', bar: 12 } // ok
1086+
```
1087+
1088+
While TypeScript does understand object spread, the support for object type spread is [not implemented](https://github.com/microsoft/TypeScript/issues/10727).
1089+
10721090
## Useful References
10731091

10741092
* https://github.com/Microsoft/TypeScript/issues/1265

0 commit comments

Comments
 (0)