assertie 0.3.0 → 0.3.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/CHANGELOG.md +6 -0
- package/README.md +22 -0
- package/package.json +1 -1
- package/manual_test/index.ts +0 -35
- package/migration-guide-template.md +0 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.3.1](https://github.com/OfficialHalfwayDead/assertie/compare/v0.3.0...v0.3.1) (2025-02-23)
|
|
4
|
+
|
|
5
|
+
* [`0d59837`](https://github.com/OfficialHalfwayDead/assertie/commit/0d59837841707c963ce174e79a527fcb1379ac4f) Add array and tuple assertions to the README.
|
|
6
|
+
|
|
7
|
+
|
|
3
8
|
## [0.3.0](https://github.com/OfficialHalfwayDead/assertie/compare/v0.2.0...v0.3.0) (2025-02-23)
|
|
4
9
|
|
|
5
10
|
### Breaking Changes
|
|
@@ -17,6 +22,7 @@
|
|
|
17
22
|
|
|
18
23
|
* [`075fecd`](https://github.com/OfficialHalfwayDead/assertie/commit/075fecd105030191eac671d8731765f4f5af2cd4) Add JSDoc comments to all functions and improve documentation overall.
|
|
19
24
|
|
|
25
|
+
|
|
20
26
|
## [0.2.0](https://github.com/OfficialHalfwayDead/assertie/compare/v0.1.0...v0.2.0) (2025-02-17)
|
|
21
27
|
|
|
22
28
|
### Breaking Changes
|
package/README.md
CHANGED
|
@@ -154,6 +154,28 @@ const safeObj = obj;
|
|
|
154
154
|
|
|
155
155
|
The reason an array is needed here is because undefined properties may not be present in `Object.keys`, so the caller needs to provide all keys to check. Don't worry about the safety, though. If you forget to pass a key, its type will remain nullable after the assert and TypeScript will not consider it safe to access.
|
|
156
156
|
|
|
157
|
+
### Arrays and tuples
|
|
158
|
+
|
|
159
|
+
Arrays and tuples have equivalent versions of `assertType` and `assertNonNullable`. These make it easy to check every element at once and get great errors and type narrowing.
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
const arr: (number | string | null)[] = [1, 2, 3];
|
|
163
|
+
assertArrayNonNullable(arr); // narrows to (number | string)[]
|
|
164
|
+
assertArrayType(arr, "number"); // narrows to number[]
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
const arr: number[] = [1, 2, 3];
|
|
169
|
+
assertIsTuple(arr, 3); // narrows to [number, number, number]
|
|
170
|
+
|
|
171
|
+
const arrMixed: (number | string | null)[] = [1, "a"];
|
|
172
|
+
assertIsTuple(arrMixed, 2); // narrows to [T, T]
|
|
173
|
+
// where T = number | string | null;
|
|
174
|
+
assertTupleNonNullable(arrMixed); // narrows T to number | string
|
|
175
|
+
assertTupleTypes(arrMixed, ["number", "string"]);
|
|
176
|
+
const tup: [number, string] = arrMixed;
|
|
177
|
+
```
|
|
178
|
+
|
|
157
179
|
### Asserting unreachable code
|
|
158
180
|
|
|
159
181
|
The unreachable assertion will
|
package/package.json
CHANGED
package/manual_test/index.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { assertArrayNonNullable, assertIsTuple, assertTupleNonNullable, assertTupleTypes, assertType } from "../src/index";
|
|
2
|
-
|
|
3
|
-
const func = (tuple: [unknown, unknown, unknown]) => {
|
|
4
|
-
console.log(tuple);
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
const tuple: [unknown, unknown, unknown] = [1, 2, 3];
|
|
8
|
-
const tuple3 = [1, 2, 3] as const;
|
|
9
|
-
const tuple1: [number | undefined, number] = [1, 2];
|
|
10
|
-
const arr: (number|null)[] = [1, 2, 3];
|
|
11
|
-
assertIsTuple(arr, 2);
|
|
12
|
-
assertTupleNonNullable(arr);
|
|
13
|
-
const nntuple1: [number, number] = arr;
|
|
14
|
-
tuple1[0] = 1;
|
|
15
|
-
// assertTupleTypes(tuple3, ["number", "number", "number"]);
|
|
16
|
-
|
|
17
|
-
type Tuple<T, N extends number, A extends unknown[] = []> = A extends { length: N } ? A : Tuple<T, N, [...A, T]>;
|
|
18
|
-
|
|
19
|
-
type Tuple20 = Tuple<number, 20>;
|
|
20
|
-
|
|
21
|
-
func(tuple);
|
|
22
|
-
|
|
23
|
-
const tuple2 = tuple3;
|
|
24
|
-
// const tuple5: [number, number, number | undefined] = tuple3;
|
|
25
|
-
|
|
26
|
-
type Tuple3 = typeof tuple3;
|
|
27
|
-
type Tuple3Length = Tuple3["length"];
|
|
28
|
-
|
|
29
|
-
function func2<T extends number[]>(
|
|
30
|
-
nums: T & (number extends T["length"] ? unknown : never)
|
|
31
|
-
) {
|
|
32
|
-
console.log(nums);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// func2(tuple3);
|