You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/docs/manual/v12.0.0/record.mdx
+82Lines changed: 82 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -29,6 +29,88 @@ type person = {
29
29
30
30
</CodeTab>
31
31
32
+
You can also nest definitions of records.
33
+
34
+
<CodeTablabels={["ReScript", "JS Output"]}>
35
+
36
+
```res prelude
37
+
type person = {
38
+
age: int,
39
+
name: string,
40
+
notificationSettings: {
41
+
sendEmails: bool,
42
+
allowPasswordLogin: bool,
43
+
},
44
+
}
45
+
46
+
let person = {
47
+
age: 90,
48
+
name: "Test Person",
49
+
notificationSettings: {
50
+
sendEmails: true,
51
+
allowPasswordLogin: false,
52
+
},
53
+
}
54
+
55
+
```
56
+
```js
57
+
let person = {
58
+
age:90,
59
+
name:"Test Person",
60
+
notificationSettings: {
61
+
sendEmails:true,
62
+
allowPasswordLogin:false
63
+
}
64
+
};
65
+
```
66
+
67
+
</CodeTab>
68
+
69
+
Nesting record definitions is a nice way to group records that are part of the same structure, and won't be referenced from the outside.
70
+
71
+
If you end up needing to refer to a nested record type explicitly, you should make it an explicit definition instead of a nested one. This is mainly for 2 reasons:
72
+
- The records that are automatically generated for the nested record definitions are named in a way that would require you to use escaped identifiers to reference them. The nested record at `notificationSettings` above would be named `\"person.notificationSettings"` for instance
73
+
- For the sake of clarity (and caring about your co-workers), having an explicit and named definition to look at and refer to is much easier than scanning a potentially large record definition for the nested record you're looking for
74
+
75
+
So if we in the example above ended up needing to refer to `person.notificationSettings` nested record from the outside, we should instead make it explicit, just like how we normally define records:
0 commit comments