Skip to content

Commit cd2e2d7

Browse files
authored
Merge pull request rescript-lang#344 from rescript-association/obj-spread
Document object type spread
2 parents d76fabd + 5784fa0 commit cd2e2d7

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

pages/docs/manual/latest/object.mdx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,40 @@ MyJSFile.student1.name = "Mary";
126126

127127
</CodeTab>
128128

129+
## Combine Types
130+
131+
You can spread one object type definition into another using `...`:
132+
133+
<CodeTab labels={["ReScript", "JS Output"]}>
134+
135+
```res example
136+
type point2d = {
137+
"x": float,
138+
"y": float,
139+
}
140+
type point3d = {
141+
...point2d,
142+
"z": float,
143+
}
144+
145+
let myPoint: point3d = {
146+
"x": 1.0,
147+
"y": 2.0,
148+
"z": 3.0,
149+
}
150+
```
151+
```js
152+
var myPoint = {
153+
x: 1.0,
154+
y: 2.0,
155+
z: 3.0
156+
};
157+
```
158+
159+
</CodeTab>
160+
161+
This only works with object types, not object values!
162+
129163
## Tips & Tricks
130164

131165
Since objects don't require type declarations, and since ReScript infers all the types for you, you get to very quickly and easily (and dangerously) bind to any JavaScript API. Check the JS output tab:

0 commit comments

Comments
 (0)