@rightcapital/assert 2.1.3-feature-assert-package-assert-function-value-parameter-type-must-be-boolean.2370.1.0 → 2.1.3-renovate-auto-merge-dev-dependencies-updates.2374.1.0
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/README.md +13 -13
- package/lib/index.d.ts +11 -11
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +20 -20
- package/lib/index.js.map +1 -1
- package/package.json +2 -2
- package/skills/assert/SKILL.md +15 -15
- package/src/index.ts +13 -13
package/README.md
CHANGED
|
@@ -95,17 +95,17 @@ assertNonNullable(user, 'User cannot be null');
|
|
|
95
95
|
// `user` is narrowed to NonNullable<User>
|
|
96
96
|
```
|
|
97
97
|
|
|
98
|
-
### Error Handling (`
|
|
98
|
+
### Error Handling (`AssertError`)
|
|
99
99
|
|
|
100
|
-
All assertion functions throw `
|
|
100
|
+
All assertion functions throw `AssertError` (which extends `Error`) when an assertion fails.
|
|
101
101
|
|
|
102
102
|
```typescript
|
|
103
|
-
import { assert,
|
|
103
|
+
import { assert, AssertError } from '@rightcapital/assert';
|
|
104
104
|
|
|
105
105
|
try {
|
|
106
106
|
assert(user.age >= 18, 'User must be at least 18 years old');
|
|
107
107
|
} catch (error) {
|
|
108
|
-
if (error instanceof
|
|
108
|
+
if (error instanceof AssertError) {
|
|
109
109
|
console.error('Assertion failed:', error.message);
|
|
110
110
|
}
|
|
111
111
|
}
|
|
@@ -113,15 +113,15 @@ try {
|
|
|
113
113
|
|
|
114
114
|
## API Summary
|
|
115
115
|
|
|
116
|
-
| API /
|
|
117
|
-
|
|
|
118
|
-
| `
|
|
119
|
-
| `assertExhaustive(value
|
|
120
|
-
| `assertUnreachable(
|
|
121
|
-
| `ensure
|
|
122
|
-
| `ensureNonNullable
|
|
123
|
-
| `assert(value
|
|
124
|
-
| `assertNonNullable
|
|
116
|
+
| API | Return / Type Behavior | Typical Use Case |
|
|
117
|
+
| :------------------------------- | :-------------------------------- | :---------------------------------------------------- |
|
|
118
|
+
| `AssertError` | `class extends Error` | Error thrown on assertion failure |
|
|
119
|
+
| `assertExhaustive(value, msg?)` | `never` | Enforce exhaustiveness check in `switch` or `if-else` |
|
|
120
|
+
| `assertUnreachable(msg?)` | `never` | Mark logically unreachable code branches |
|
|
121
|
+
| `ensure(value, predicate, msg?)` | `S extends T` | Validate and return value with narrowed type |
|
|
122
|
+
| `ensureNonNullable(value, msg?)` | `NonNullable<T>` | Validate non-null/undefined and return value |
|
|
123
|
+
| `assert(value, msg?)` | `asserts value` | Standard boolean condition assertion |
|
|
124
|
+
| `assertNonNullable(value, msg?)` | `asserts value is NonNullable<T>` | Standard non-null/undefined assertion |
|
|
125
125
|
|
|
126
126
|
## Agent Skills
|
|
127
127
|
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Assertion functions provide type-safe assertions and validation.
|
|
3
|
-
* When assertions fail, they throw `
|
|
3
|
+
* When assertions fail, they throw `AssertError`.
|
|
4
4
|
*
|
|
5
5
|
* @author lixiaoyan <lxy.lixiaoyan@gmail.com>
|
|
6
6
|
* @example
|
|
@@ -20,15 +20,15 @@
|
|
|
20
20
|
/**
|
|
21
21
|
* Error thrown when an assertion fails.
|
|
22
22
|
*/
|
|
23
|
-
export declare class
|
|
24
|
-
readonly name = "
|
|
23
|
+
export declare class AssertError extends Error {
|
|
24
|
+
readonly name = "AssertError";
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
27
|
* Basic assertion: verifies that a value or expression is `true`, otherwise throws an exception.
|
|
28
28
|
*
|
|
29
|
-
* @param value - The
|
|
29
|
+
* @param value - The value to assert as truthy
|
|
30
30
|
* @param message - Optional custom error message
|
|
31
|
-
* @throws {
|
|
31
|
+
* @throws {AssertError} Throws an error if `value` is not `true`.
|
|
32
32
|
*
|
|
33
33
|
* @example
|
|
34
34
|
* ```typescript
|
|
@@ -40,14 +40,14 @@ export declare class AssertionError extends Error {
|
|
|
40
40
|
* assert(isValid, 'Data validation failed');
|
|
41
41
|
* ```
|
|
42
42
|
*/
|
|
43
|
-
export declare function assert(value:
|
|
43
|
+
export declare function assert(value: unknown, message?: string): asserts value;
|
|
44
44
|
/**
|
|
45
45
|
* Asserts that a value is not `null` or `undefined`, providing TypeScript type narrowing to `NonNullable<T>`.
|
|
46
46
|
* Ensures subsequent code can safely access the value.
|
|
47
47
|
*
|
|
48
48
|
* @param value - The value to check for null/undefined
|
|
49
49
|
* @param message - Optional custom error message
|
|
50
|
-
* @throws {
|
|
50
|
+
* @throws {AssertError} Throws an error if `value` is `null` or `undefined`.
|
|
51
51
|
*
|
|
52
52
|
* @example
|
|
53
53
|
* ```typescript
|
|
@@ -66,7 +66,7 @@ export declare function assertNonNullable<T>(value: T, message?: string): assert
|
|
|
66
66
|
* @param predicate - Type guard function that validates the value
|
|
67
67
|
* @param message - Optional custom error message
|
|
68
68
|
* @returns The value with narrowed type
|
|
69
|
-
* @throws {
|
|
69
|
+
* @throws {AssertError} Throws an error if `predicate` returns `false`.
|
|
70
70
|
*
|
|
71
71
|
* @example
|
|
72
72
|
* ```typescript
|
|
@@ -91,7 +91,7 @@ export declare function ensure<T, S extends T>(value: T, predicate: (value: T) =
|
|
|
91
91
|
* @param value - The value to check for null/undefined
|
|
92
92
|
* @param message - Optional custom error message
|
|
93
93
|
* @returns The non-nullable value
|
|
94
|
-
* @throws {
|
|
94
|
+
* @throws {AssertError} Throws an error if `value` is `null` or `undefined`.
|
|
95
95
|
*
|
|
96
96
|
* @example
|
|
97
97
|
* ```typescript
|
|
@@ -112,7 +112,7 @@ export declare function ensureNonNullable<T>(value: T, message?: string): NonNul
|
|
|
112
112
|
*
|
|
113
113
|
* @param message - Optional custom error message
|
|
114
114
|
* @returns Never returns (always throws)
|
|
115
|
-
* @throws {
|
|
115
|
+
* @throws {AssertError} Always thrown.
|
|
116
116
|
*
|
|
117
117
|
* @example
|
|
118
118
|
* ```typescript
|
|
@@ -136,7 +136,7 @@ export declare function assertUnreachable(message?: string): never;
|
|
|
136
136
|
* @param value - The value that should be `never` if all cases are handled
|
|
137
137
|
* @param message - Optional custom error message
|
|
138
138
|
* @returns Never returns (always throws)
|
|
139
|
-
* @throws {
|
|
139
|
+
* @throws {AssertError} Always thrown.
|
|
140
140
|
*
|
|
141
141
|
* @example
|
|
142
142
|
* ```typescript
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH;;GAEG;AACH,qBAAa,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH;;GAEG;AACH,qBAAa,WAAY,SAAQ,KAAK;IACpC,SAAyB,IAAI,iBAAiB;CAC/C;AAaD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,CAItE;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EACjC,KAAK,EAAE,CAAC,EACR,OAAO,CAAC,EAAE,MAAM,GACf,OAAO,CAAC,KAAK,IAAI,WAAW,CAAC,CAAC,CAAC,CAIjC;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,CAAC,SAAS,CAAC,EACnC,KAAK,EAAE,CAAC,EACR,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,KAAK,IAAI,CAAC,EACnC,OAAO,CAAC,EAAE,MAAM,GACf,CAAC,CAGH;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EACjC,KAAK,EAAE,CAAC,EACR,OAAO,CAAC,EAAE,MAAM,GACf,WAAW,CAAC,CAAC,CAAC,CAKhB;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,KAAK,CAEzD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,KAAK,CAEtE"}
|
package/lib/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
3
|
* Assertion functions provide type-safe assertions and validation.
|
|
4
|
-
* When assertions fail, they throw `
|
|
4
|
+
* When assertions fail, they throw `AssertError`.
|
|
5
5
|
*
|
|
6
6
|
* @author lixiaoyan <lxy.lixiaoyan@gmail.com>
|
|
7
7
|
* @example
|
|
@@ -19,29 +19,35 @@
|
|
|
19
19
|
* ```
|
|
20
20
|
*/
|
|
21
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
exports.
|
|
22
|
+
exports.AssertError = void 0;
|
|
23
|
+
exports.assert = assert;
|
|
24
|
+
exports.assertNonNullable = assertNonNullable;
|
|
25
|
+
exports.ensure = ensure;
|
|
26
|
+
exports.ensureNonNullable = ensureNonNullable;
|
|
27
|
+
exports.assertUnreachable = assertUnreachable;
|
|
28
|
+
exports.assertExhaustive = assertExhaustive;
|
|
23
29
|
/**
|
|
24
30
|
* Error thrown when an assertion fails.
|
|
25
31
|
*/
|
|
26
|
-
class
|
|
32
|
+
class AssertError extends Error {
|
|
27
33
|
constructor() {
|
|
28
34
|
super(...arguments);
|
|
29
|
-
this.name = '
|
|
35
|
+
this.name = 'AssertError';
|
|
30
36
|
}
|
|
31
37
|
}
|
|
32
|
-
exports.
|
|
38
|
+
exports.AssertError = AssertError;
|
|
33
39
|
/**
|
|
34
|
-
* Throws an
|
|
40
|
+
* Throws an AssertError for failed assertions.
|
|
35
41
|
*/
|
|
36
42
|
function throwError(name, value, message) {
|
|
37
|
-
throw new
|
|
43
|
+
throw new AssertError(message !== null && message !== void 0 ? message : `${name}: Unexpected ${String(value)}`);
|
|
38
44
|
}
|
|
39
45
|
/**
|
|
40
46
|
* Basic assertion: verifies that a value or expression is `true`, otherwise throws an exception.
|
|
41
47
|
*
|
|
42
|
-
* @param value - The
|
|
48
|
+
* @param value - The value to assert as truthy
|
|
43
49
|
* @param message - Optional custom error message
|
|
44
|
-
* @throws {
|
|
50
|
+
* @throws {AssertError} Throws an error if `value` is not `true`.
|
|
45
51
|
*
|
|
46
52
|
* @example
|
|
47
53
|
* ```typescript
|
|
@@ -58,14 +64,13 @@ function assert(value, message) {
|
|
|
58
64
|
throwError('assert', value, message);
|
|
59
65
|
}
|
|
60
66
|
}
|
|
61
|
-
exports.assert = assert;
|
|
62
67
|
/**
|
|
63
68
|
* Asserts that a value is not `null` or `undefined`, providing TypeScript type narrowing to `NonNullable<T>`.
|
|
64
69
|
* Ensures subsequent code can safely access the value.
|
|
65
70
|
*
|
|
66
71
|
* @param value - The value to check for null/undefined
|
|
67
72
|
* @param message - Optional custom error message
|
|
68
|
-
* @throws {
|
|
73
|
+
* @throws {AssertError} Throws an error if `value` is `null` or `undefined`.
|
|
69
74
|
*
|
|
70
75
|
* @example
|
|
71
76
|
* ```typescript
|
|
@@ -81,7 +86,6 @@ function assertNonNullable(value, message) {
|
|
|
81
86
|
throwError('assertNonNullable', value, message);
|
|
82
87
|
}
|
|
83
88
|
}
|
|
84
|
-
exports.assertNonNullable = assertNonNullable;
|
|
85
89
|
/**
|
|
86
90
|
* Similar to `assert`, but returns the value. Ensures a value matches a type predicate and returns it with narrowed type.
|
|
87
91
|
*
|
|
@@ -89,7 +93,7 @@ exports.assertNonNullable = assertNonNullable;
|
|
|
89
93
|
* @param predicate - Type guard function that validates the value
|
|
90
94
|
* @param message - Optional custom error message
|
|
91
95
|
* @returns The value with narrowed type
|
|
92
|
-
* @throws {
|
|
96
|
+
* @throws {AssertError} Throws an error if `predicate` returns `false`.
|
|
93
97
|
*
|
|
94
98
|
* @example
|
|
95
99
|
* ```typescript
|
|
@@ -111,14 +115,13 @@ function ensure(value, predicate, message) {
|
|
|
111
115
|
assert(predicate(value), message);
|
|
112
116
|
return value;
|
|
113
117
|
}
|
|
114
|
-
exports.ensure = ensure;
|
|
115
118
|
/**
|
|
116
119
|
* Similar to `assertNonNullable`, but returns the value. Ensures a value is not null/undefined and returns it.
|
|
117
120
|
*
|
|
118
121
|
* @param value - The value to check for null/undefined
|
|
119
122
|
* @param message - Optional custom error message
|
|
120
123
|
* @returns The non-nullable value
|
|
121
|
-
* @throws {
|
|
124
|
+
* @throws {AssertError} Throws an error if `value` is `null` or `undefined`.
|
|
122
125
|
*
|
|
123
126
|
* @example
|
|
124
127
|
* ```typescript
|
|
@@ -138,14 +141,13 @@ function ensureNonNullable(value, message) {
|
|
|
138
141
|
}
|
|
139
142
|
return value;
|
|
140
143
|
}
|
|
141
|
-
exports.ensureNonNullable = ensureNonNullable;
|
|
142
144
|
/**
|
|
143
145
|
* Marks code branches that should theoretically never be reached.
|
|
144
146
|
* Used for defensive programming to prevent unexpected code execution when data or logic doesn't match expectations.
|
|
145
147
|
*
|
|
146
148
|
* @param message - Optional custom error message
|
|
147
149
|
* @returns Never returns (always throws)
|
|
148
|
-
* @throws {
|
|
150
|
+
* @throws {AssertError} Always thrown.
|
|
149
151
|
*
|
|
150
152
|
* @example
|
|
151
153
|
* ```typescript
|
|
@@ -164,7 +166,6 @@ exports.ensureNonNullable = ensureNonNullable;
|
|
|
164
166
|
function assertUnreachable(message) {
|
|
165
167
|
throwError('assertUnreachable', null, message);
|
|
166
168
|
}
|
|
167
|
-
exports.assertUnreachable = assertUnreachable;
|
|
168
169
|
/**
|
|
169
170
|
* Used for exhaustiveness checking of union types. Ensures switch or if-else statements cover all possible types.
|
|
170
171
|
* Leverages TypeScript's `never` type to catch missing branches at compile time.
|
|
@@ -172,7 +173,7 @@ exports.assertUnreachable = assertUnreachable;
|
|
|
172
173
|
* @param value - The value that should be `never` if all cases are handled
|
|
173
174
|
* @param message - Optional custom error message
|
|
174
175
|
* @returns Never returns (always throws)
|
|
175
|
-
* @throws {
|
|
176
|
+
* @throws {AssertError} Always thrown.
|
|
176
177
|
*
|
|
177
178
|
* @example
|
|
178
179
|
* ```typescript
|
|
@@ -201,5 +202,4 @@ exports.assertUnreachable = assertUnreachable;
|
|
|
201
202
|
function assertExhaustive(value, message) {
|
|
202
203
|
throwError('assertExhaustive', value, message);
|
|
203
204
|
}
|
|
204
|
-
exports.assertExhaustive = assertExhaustive;
|
|
205
205
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;GAkBG;;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;GAkBG;;;AAqCH,wBAIC;AAmBD,8CAOC;AA2BD,wBAOC;AAsBD,8CAQC;AAwBD,8CAEC;AAmCD,4CAEC;AAhMD;;GAEG;AACH,MAAa,WAAY,SAAQ,KAAK;IAAtC;;QAC2B,SAAI,GAAG,aAAa,CAAC;IAChD,CAAC;CAAA;AAFD,kCAEC;AAED;;GAEG;AACH,SAAS,UAAU,CACjB,IAAY,EACZ,KAAc,EACd,OAA2B;IAE3B,MAAM,IAAI,WAAW,CAAC,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,GAAG,IAAI,gBAAgB,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;AAC3E,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,MAAM,CAAC,KAAc,EAAE,OAAgB;IACrD,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QACnB,UAAU,CAAC,QAAQ,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;IACvC,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,iBAAiB,CAC/B,KAAQ,EACR,OAAgB;IAEhB,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QAC1C,UAAU,CAAC,mBAAmB,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;IAClD,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,SAAgB,MAAM,CACpB,KAAQ,EACR,SAAmC,EACnC,OAAgB;IAEhB,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC;IAClC,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,iBAAiB,CAC/B,KAAQ,EACR,OAAgB;IAEhB,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QAC1C,UAAU,CAAC,mBAAmB,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;IAClD,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,SAAgB,iBAAiB,CAAC,OAAgB;IAChD,UAAU,CAAC,mBAAmB,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;AACjD,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,SAAgB,gBAAgB,CAAC,KAAY,EAAE,OAAgB;IAC7D,UAAU,CAAC,kBAAkB,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;AACjD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rightcapital/assert",
|
|
3
|
-
"version": "2.1.3-
|
|
3
|
+
"version": "2.1.3-renovate-auto-merge-dev-dependencies-updates.2374.1.0",
|
|
4
4
|
"description": "Type-safe assertion utilities for defensive programming.",
|
|
5
5
|
"author": "RightCapital Ecosystem team <npm-publisher@rightcapital.com>",
|
|
6
6
|
"keywords": [
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"registry": "https://registry.npmjs.org"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"typedoc": "0.
|
|
36
|
+
"typedoc": "0.28.20",
|
|
37
37
|
"typedoc-plugin-markdown": "3.17.1"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
package/skills/assert/SKILL.md
CHANGED
|
@@ -12,14 +12,14 @@ metadata:
|
|
|
12
12
|
|
|
13
13
|
# assert
|
|
14
14
|
|
|
15
|
-
Type-safe assertion utilities for defensive TypeScript programming. All functions throw `
|
|
15
|
+
Type-safe assertion utilities for defensive TypeScript programming. All functions throw `AssertError` (which extends `Error`) on failure. Default error message format is `${functionName}: Unexpected ${String(value)}`.
|
|
16
16
|
|
|
17
17
|
## Import
|
|
18
18
|
|
|
19
19
|
```typescript
|
|
20
20
|
import {
|
|
21
21
|
assert,
|
|
22
|
-
|
|
22
|
+
AssertError,
|
|
23
23
|
assertExhaustive,
|
|
24
24
|
assertNonNullable,
|
|
25
25
|
assertUnreachable,
|
|
@@ -30,27 +30,27 @@ import {
|
|
|
30
30
|
|
|
31
31
|
## Quick Reference
|
|
32
32
|
|
|
33
|
-
|
|
|
34
|
-
|
|
|
35
|
-
| `
|
|
36
|
-
| `assert(value
|
|
37
|
-
| `assertNonNullable
|
|
38
|
-
| `ensure
|
|
39
|
-
| `ensureNonNullable
|
|
40
|
-
| `assertExhaustive(value
|
|
41
|
-
| `assertUnreachable(
|
|
33
|
+
| Class / Function | Signature / Return | Use Case |
|
|
34
|
+
| -------------------------------- | --------------------------------- | ------------------------------------------------------------------- |
|
|
35
|
+
| `AssertError` | `class extends Error` | Error type thrown when any assertion in this package fails. |
|
|
36
|
+
| `assert(value, msg?)` | `asserts value` | Precondition or boolean check (verifies `value === true`). |
|
|
37
|
+
| `assertNonNullable(value, msg?)` | `asserts value is NonNullable<T>` | Guard statement against `null` or `undefined`. |
|
|
38
|
+
| `ensure(value, predicate, msg?)` | `S extends T` | Inline validation using type guard function `(val: T) => val is S`. |
|
|
39
|
+
| `ensureNonNullable(value, msg?)` | `NonNullable<T>` | Inline assignment or method chain for non-null value. |
|
|
40
|
+
| `assertExhaustive(value, msg?)` | `value: never` -> `never` | Exhaustiveness check in `switch` `default` case or `if-else` chain. |
|
|
41
|
+
| `assertUnreachable(msg?)` | `never` | Mark logically impossible code paths. |
|
|
42
42
|
|
|
43
|
-
## Error Handling with `
|
|
43
|
+
## Error Handling with `AssertError`
|
|
44
44
|
|
|
45
|
-
All assertion functions throw `
|
|
45
|
+
All assertion functions throw `AssertError` when a condition is not met. Use `instanceof AssertError` to catch assertion failures specifically:
|
|
46
46
|
|
|
47
47
|
```typescript
|
|
48
|
-
import { assert,
|
|
48
|
+
import { assert, AssertError } from '@rightcapital/assert';
|
|
49
49
|
|
|
50
50
|
try {
|
|
51
51
|
assert(age >= 18, 'User must be an adult');
|
|
52
52
|
} catch (error) {
|
|
53
|
-
if (error instanceof
|
|
53
|
+
if (error instanceof AssertError) {
|
|
54
54
|
console.error('Assertion failed:', error.message);
|
|
55
55
|
} else {
|
|
56
56
|
throw error;
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Assertion functions provide type-safe assertions and validation.
|
|
3
|
-
* When assertions fail, they throw `
|
|
3
|
+
* When assertions fail, they throw `AssertError`.
|
|
4
4
|
*
|
|
5
5
|
* @author lixiaoyan <lxy.lixiaoyan@gmail.com>
|
|
6
6
|
* @example
|
|
@@ -21,27 +21,27 @@
|
|
|
21
21
|
/**
|
|
22
22
|
* Error thrown when an assertion fails.
|
|
23
23
|
*/
|
|
24
|
-
export class
|
|
25
|
-
public override readonly name = '
|
|
24
|
+
export class AssertError extends Error {
|
|
25
|
+
public override readonly name = 'AssertError';
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
|
-
* Throws an
|
|
29
|
+
* Throws an AssertError for failed assertions.
|
|
30
30
|
*/
|
|
31
31
|
function throwError(
|
|
32
32
|
name: string,
|
|
33
33
|
value: unknown,
|
|
34
34
|
message: string | undefined,
|
|
35
35
|
): never {
|
|
36
|
-
throw new
|
|
36
|
+
throw new AssertError(message ?? `${name}: Unexpected ${String(value)}`);
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
/**
|
|
40
40
|
* Basic assertion: verifies that a value or expression is `true`, otherwise throws an exception.
|
|
41
41
|
*
|
|
42
|
-
* @param value - The
|
|
42
|
+
* @param value - The value to assert as truthy
|
|
43
43
|
* @param message - Optional custom error message
|
|
44
|
-
* @throws {
|
|
44
|
+
* @throws {AssertError} Throws an error if `value` is not `true`.
|
|
45
45
|
*
|
|
46
46
|
* @example
|
|
47
47
|
* ```typescript
|
|
@@ -53,7 +53,7 @@ function throwError(
|
|
|
53
53
|
* assert(isValid, 'Data validation failed');
|
|
54
54
|
* ```
|
|
55
55
|
*/
|
|
56
|
-
export function assert(value:
|
|
56
|
+
export function assert(value: unknown, message?: string): asserts value {
|
|
57
57
|
if (value !== true) {
|
|
58
58
|
throwError('assert', value, message);
|
|
59
59
|
}
|
|
@@ -65,7 +65,7 @@ export function assert(value: boolean, message?: string): asserts value {
|
|
|
65
65
|
*
|
|
66
66
|
* @param value - The value to check for null/undefined
|
|
67
67
|
* @param message - Optional custom error message
|
|
68
|
-
* @throws {
|
|
68
|
+
* @throws {AssertError} Throws an error if `value` is `null` or `undefined`.
|
|
69
69
|
*
|
|
70
70
|
* @example
|
|
71
71
|
* ```typescript
|
|
@@ -92,7 +92,7 @@ export function assertNonNullable<T>(
|
|
|
92
92
|
* @param predicate - Type guard function that validates the value
|
|
93
93
|
* @param message - Optional custom error message
|
|
94
94
|
* @returns The value with narrowed type
|
|
95
|
-
* @throws {
|
|
95
|
+
* @throws {AssertError} Throws an error if `predicate` returns `false`.
|
|
96
96
|
*
|
|
97
97
|
* @example
|
|
98
98
|
* ```typescript
|
|
@@ -125,7 +125,7 @@ export function ensure<T, S extends T>(
|
|
|
125
125
|
* @param value - The value to check for null/undefined
|
|
126
126
|
* @param message - Optional custom error message
|
|
127
127
|
* @returns The non-nullable value
|
|
128
|
-
* @throws {
|
|
128
|
+
* @throws {AssertError} Throws an error if `value` is `null` or `undefined`.
|
|
129
129
|
*
|
|
130
130
|
* @example
|
|
131
131
|
* ```typescript
|
|
@@ -155,7 +155,7 @@ export function ensureNonNullable<T>(
|
|
|
155
155
|
*
|
|
156
156
|
* @param message - Optional custom error message
|
|
157
157
|
* @returns Never returns (always throws)
|
|
158
|
-
* @throws {
|
|
158
|
+
* @throws {AssertError} Always thrown.
|
|
159
159
|
*
|
|
160
160
|
* @example
|
|
161
161
|
* ```typescript
|
|
@@ -182,7 +182,7 @@ export function assertUnreachable(message?: string): never {
|
|
|
182
182
|
* @param value - The value that should be `never` if all cases are handled
|
|
183
183
|
* @param message - Optional custom error message
|
|
184
184
|
* @returns Never returns (always throws)
|
|
185
|
-
* @throws {
|
|
185
|
+
* @throws {AssertError} Always thrown.
|
|
186
186
|
*
|
|
187
187
|
* @example
|
|
188
188
|
* ```typescript
|