Skip to content

Commit fe39a63

Browse files
committed
Add optional shape fields docs
1 parent 34ec9a3 commit fe39a63

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

Writerside/topics/language/shape-types.md

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ array{
5353
> second
5454
> }
5555
> ```
56+
>
57+
> An error similar to the one below should occur.
5658
> ```
57-
> Cannot mix numeric and named keys.
59+
> ParseException: Cannot mix numeric and named keys.
5860
> ```
59-
> {collapsible="true" collapsed-title="TypeLang\Parser\Exception\ParseException"}
6061
{style="warning"}
6162
6263
> Support for other types of **keys**, such as const mask (`Class::CONST_*`)
@@ -67,12 +68,34 @@ array{
6768
> ...
6869
> }
6970
> ```
71+
>
72+
> An error similar to the one below should occur.
7073
> ```
71-
> Syntax error, unexpected ":"
74+
> ParseException: Syntax error, unexpected ":"
7275
> ```
73-
> {collapsible="true" collapsed-title="TypeLang\Parser\Exception\ParseException"}
7476
{style="warning"}
7577
78+
### Optional Fields
79+
80+
Specifying fields (keys) allows the optionality of the presence of a field.
81+
Such fields are indicated by a question mark (`?`) **before** the colon (`:`)
82+
symbol: `key?: Type`.
83+
84+
<compare first-title="Optional Key" second-title="Optional Value">
85+
86+
```php
87+
array{
88+
key?: Type,
89+
}
90+
```
91+
92+
```php
93+
array{
94+
key: Type?,
95+
}
96+
```
97+
</compare>
98+
7699
### Unsealed Shapes
77100

78101
Unsealed (unclosed) shapes mean that the composite type can contain additional
@@ -168,10 +191,11 @@ An attribute is additional metadata for a field.
168191
> test?: App\Domain\User,
169192
> }
170193
> ```
194+
>
195+
> An error similar to the one below should occur
171196
> ```
172-
> Syntax error, unexpected "42"
197+
> ParseException: Syntax error, unexpected "42"
173198
> ```
174-
> {collapsible="true" collapsed-title="TypeLang\Parser\Exception\ParseException"}
175199
> {style="warning"}
176200
177201
</tab>

0 commit comments

Comments
 (0)