File: 0.29.2.md

package info (click to toggle)
node-sinclair-typebox 0.34.41-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 6,948 kB
  • sloc: makefile: 4
file content (141 lines) | stat: -rw-r--r-- 3,620 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
## [0.29.2](https://www.npmjs.com/package/@sinclair/typebox/v/0.29.2)

## Overview

Revision 0.29.2 includes enhancements to `Type.Index`

This revision contains no breaking changes

## Contents

- Enhancements
  - [Modifier Index Resolver](#Modifier-Index-Resolver)
  - [Indexed Intersect](#Indexed-Intersect)
  - [Indexed Union](#Indexed-Union)
  - [Composite](#Composite)

<a name="Modifier-Index-Resolver"></a>

## Modifier Index Resolver

Revision 0.29.2 re-introduces optional property narrowing for Indexed Access Types. This functionality is specific when indexing overlapping properties with one or more optional modifiers. Revision 0.28.0 attempted to implement this narrowing, however was pulled due to instantiation issues raised on issue [419](https://github.com/sinclairzx81/typebox/issues/419) (specific to composite narrowing)

Revision 0.29.2 attempts to re-introduce this functionality using a different resolution strategy. It uses Indexed Access Types as the construct in which to apply such narrowing and expands upon Union and Intersection type normalization for optional modifier unwrap and remapping. This approach unifies Composite inference with Index Access Types and makes provisions for a possible `Type.Mapped` feature in later releases.

<a name="Indexed-Intersect"></a>

## Indexed Intersect

The following are the index resolver cases for intersect types.

### Case 1
```typescript
const T = Type.Intersect([
  Type.Object({ x: Type.Optional(Type.Number()) }),
  Type.Object({ x: Type.Optional(Type.Number()) })
])

const I = Type.Index(T, ['x']) 

// type I = TOptional<TUnion<[TNumber, TNumber]>>
```
### Case 2
```typescript
const T = Type.Intersect([
  Type.Object({ x: Type.Optional(Type.Number()) }),
  Type.Object({ x: Type.Number() })
])

const I = Type.Index(T, ['x']) 

// type I = TUnion<[TNumber, TNumber]>
```
### Case 3
```typescript
const T = Type.Intersect([
  Type.Object({ x: Type.Number() }),
  Type.Object({ x: Type.Number() })
])

const I = Type.Index(T, ['x']) 

// type I = TUnion<[TNumber, TNumber]>
```
<a name="Indexed-Union"></a>

## Indexed Union

The following are the index resolver cases for union types.

### Case 1
```typescript
const T = Type.Union([
  Type.Object({ x: Type.Optional(Type.Number()) }),
  Type.Object({ x: Type.Optional(Type.Number()) })
])

const I = Type.Index(T, ['x']) 

// type I = TOptional<TUnion<[TNumber, TNumber]>>
```
### Case 2
```typescript
const T = Type.Union([
  Type.Object({ x: Type.Optional(Type.Number()) }),
  Type.Object({ x: Type.Number() })
])

const I = Type.Index(T, ['x']) 

// type I = TOptional<TUnion<[TNumber, TNumber]>>
```
### Case 3
```typescript
const T = Type.Union([
  Type.Object({ x: Type.Number() }),
  Type.Object({ x: Type.Number() })
])

const I = Type.Index(T, ['x']) 

// type I = TUnion<[TNumber, TNumber]>
```
<a name="Composite"></a>

## Composite

The following are the resolver cases for indexed types when applied to composite intersection.

### Case 1
```typescript
const T = Type.Composite([
  Type.Object({ x: Type.Optional(Type.Number()) }),
  Type.Object({ x: Type.Optional(Type.Number()) })
])

// type T = TObject<{
//    x: TOptional<TUnion<[TNumber, TNumber]>>
// }>
```
### Case 2
```typescript
const T = Type.Composite([
  Type.Object({ x: Type.Optional(Type.Number()) }),
  Type.Object({ x: Type.Number() })
])

// type T = TObject<{
//   x: TUnion<[TNumber, TNumber]>
// }>
```
### Case 3
```typescript
const T = Type.Composite([
  Type.Object({ x: Type.Number() }),
  Type.Object({ x: Type.Number() })
])

// type T = TObject<{
//   x: TUnion<[TNumber, TNumber]>
// }>
```