|
| 1 | +# Type Offsets |
| 2 | + |
| 3 | +<show-structure for="chapter" depth="2"/> |
| 4 | +<secondary-label ref="tl1.4"/> |
| 5 | + |
| 6 | +To specify a reference to a specific <a href="shape-types.md">shape or array element</a>, |
| 7 | +the offset access syntax is used. |
| 8 | + |
| 9 | +A description of this type consists of two parts: Arbitrary type description; |
| 10 | +The type of specific element, enclosed in square `[]` brackets. |
| 11 | + |
| 12 | +<tabs> |
| 13 | +<tab title="Examples"> |
| 14 | + |
| 15 | +> Type with string key "offset". |
| 16 | +> ```typescript |
| 17 | +> T['offset'] |
| 18 | +> ``` |
| 19 | +
|
| 20 | +> Type with dependent key. |
| 21 | +> ```typescript |
| 22 | +> T[U] |
| 23 | +> ``` |
| 24 | +
|
| 25 | +> <a href="shape-types.md">Shape type</a> with numeric key. |
| 26 | +> ```typescript |
| 27 | +> array{int, string}[0] |
| 28 | +> ``` |
| 29 | +
|
| 30 | +> Complex example using <a href="shape-types.md">shape types</a> |
| 31 | +> and <a href="generic-types.md">generics</a>. |
| 32 | +> ```typescript |
| 33 | +> T<U>[object{key: int, ...}] |
| 34 | +> ``` |
| 35 | +
|
| 36 | +</tab> |
| 37 | +<tab title="Counterexamples"> |
| 38 | +
|
| 39 | +> The offset is enclosed in single brackets. |
| 40 | +> ```typescript |
| 41 | +> Collection[[Some]] |
| 42 | +> ``` |
| 43 | +> ``` |
| 44 | +> Syntax error, unexpected "[" |
| 45 | +> ``` |
| 46 | +> {collapsible="true" collapsed-title="TypeLang\Parser\Exception\ParseException"} |
| 47 | +> {style="warning"} |
| 48 | +
|
| 49 | +> There must be a type description first, followed |
| 50 | +> by a reference to an element of that type. |
| 51 | +> ```typescript |
| 52 | +> Collection['key']{key: string} |
| 53 | +> ``` |
| 54 | +> ``` |
| 55 | +> Syntax error, unexpected "{" |
| 56 | +> ``` |
| 57 | +> {collapsible="true" collapsed-title="TypeLang\Parser\Exception\ParseException"} |
| 58 | +> {style="warning"} |
| 59 | +
|
| 60 | +</tab> |
| 61 | +</tabs> |
| 62 | +
|
| 63 | +<note> |
| 64 | +Note that since this is a type description language, any key type is allowed, |
| 65 | +including objects, <a href="conditional-types.md">conditions</a> or |
| 66 | +<a href="shape-types.md">shapes</a>. |
| 67 | +</note> |
0 commit comments