tuple-ts 0.7.1 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENCE +7 -0
- package/README.md +15 -15
- package/dist/hash.d.ts +0 -1
- package/dist/index.d.ts +4106 -4107
- package/dist/index.js +7 -22
- package/dist/intern-cache.d.ts +0 -1
- package/package.json +16 -8
- package/dist/hash.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/intern-cache.d.ts.map +0 -1
package/LICENCE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright 2026 George Bradley
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
CHANGED
|
@@ -78,9 +78,9 @@ Type-narrowed tuple factories are also supported:
|
|
|
78
78
|
|
|
79
79
|
```ts
|
|
80
80
|
type Point = [number, number];
|
|
81
|
-
const Point = tuple.
|
|
81
|
+
const Point = tuple.typed<Point>();
|
|
82
82
|
// Or equivalently, using an exemplar:
|
|
83
|
-
// const Point = tuple.
|
|
83
|
+
// const Point = tuple.typed(0, 0);
|
|
84
84
|
|
|
85
85
|
type P = Parameters<typeof Point>; // [number, number];
|
|
86
86
|
```
|
|
@@ -102,14 +102,14 @@ tuple(10, 20) === tuple(10, 21); // false!
|
|
|
102
102
|
tuple(10, 20) === tuple(20, 10); // false!
|
|
103
103
|
```
|
|
104
104
|
|
|
105
|
-
### Constraining a tuple factory with `.
|
|
105
|
+
### Constraining a tuple factory with `.typed()`
|
|
106
106
|
|
|
107
|
-
Use `.
|
|
107
|
+
Use `.typed()` to make a reusable tuple factory whose arguments must satisfy a nominated tuple type.
|
|
108
108
|
|
|
109
109
|
Specify the type explicitly:
|
|
110
110
|
|
|
111
111
|
```ts
|
|
112
|
-
const Point = tuple.
|
|
112
|
+
const Point = tuple.typed<[number, number]>();
|
|
113
113
|
|
|
114
114
|
Point(10, 20); // OK
|
|
115
115
|
Point(10, "20"); // TypeScript error
|
|
@@ -118,7 +118,7 @@ Point(10, "20"); // TypeScript error
|
|
|
118
118
|
You can alternatively provide an exemplar from which the type constraint will be inferred:
|
|
119
119
|
|
|
120
120
|
```ts
|
|
121
|
-
const NumberPair = tuple.
|
|
121
|
+
const NumberPair = tuple.typed(0, 0);
|
|
122
122
|
|
|
123
123
|
NumberPair(10, 20); // OK
|
|
124
124
|
NumberPair(10, "20"); // TypeScript error
|
|
@@ -193,40 +193,40 @@ The returned value contains the tuple slots and the schema's named properties, n
|
|
|
193
193
|
|
|
194
194
|
### Constraining a struct factory
|
|
195
195
|
|
|
196
|
-
Just like `tuple`, structs support `.
|
|
196
|
+
Just like `tuple`, structs support `.typed()` to specify the struct's value types positionally:
|
|
197
197
|
|
|
198
198
|
```ts
|
|
199
|
-
const Point = struct(["x", "y"]).
|
|
199
|
+
const Point = struct(["x", "y"]).typed<[number, number]>();
|
|
200
200
|
|
|
201
201
|
Point(10, 20); // OK
|
|
202
202
|
Point(10, "20"); // TypeScript error
|
|
203
203
|
```
|
|
204
204
|
|
|
205
|
-
Use `.
|
|
205
|
+
Use `.typedFrom()` when you want to express the value types by label instead:
|
|
206
206
|
|
|
207
207
|
```ts
|
|
208
208
|
type Coordinates = { x: number; y: number };
|
|
209
209
|
|
|
210
|
-
const Point = struct(["x", "y"]).
|
|
210
|
+
const Point = struct(["x", "y"]).typedFrom<Coordinates>();
|
|
211
211
|
|
|
212
212
|
Point(10, 20); // OK
|
|
213
213
|
Point(10, "20"); // TypeScript error
|
|
214
214
|
Point.from({ x: 10, y: 20 }); // OK
|
|
215
215
|
```
|
|
216
216
|
|
|
217
|
-
You can also provide a typed exemplar to `.
|
|
217
|
+
You can also provide a typed exemplar to `.typedFrom()`:
|
|
218
218
|
|
|
219
219
|
```ts
|
|
220
|
-
const Point = struct(["x", "y"]).
|
|
220
|
+
const Point = struct(["x", "y"]).typedFrom({ x: 0, y: 0 });
|
|
221
221
|
```
|
|
222
222
|
|
|
223
223
|
### Struct schemas and identity
|
|
224
224
|
|
|
225
|
-
Each call to `struct(labels)` creates its own cache. Factories narrowed from that schema share the cache, but separate calls to `struct()` create separate caches, even when given identical labels:
|
|
225
|
+
Each call to `struct(labels)` creates its own cache. Factories narrowed from that schema with `.typed()` or `.typedFrom()` share the cache, but separate calls to `struct()` create separate caches, even when given identical labels:
|
|
226
226
|
|
|
227
227
|
```ts
|
|
228
|
-
const PointA = struct(["x", "y"]).
|
|
229
|
-
const PointB = struct(["x", "y"]).
|
|
228
|
+
const PointA = struct(["x", "y"]).typed<[number, number]>();
|
|
229
|
+
const PointB = struct(["x", "y"]).typed<[number, number]>();
|
|
230
230
|
|
|
231
231
|
PointA(1, 2) === PointA(1, 2); // true
|
|
232
232
|
PointA(1, 2) === PointB(1, 2); // false
|
package/dist/hash.d.ts
CHANGED