type-guardians 1.1.1 → 1.2.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 +3 -3
- package/number.ts +5 -2
- package/package.json +2 -1
- package/string.ts +5 -2
package/README.md
CHANGED
|
@@ -37,7 +37,7 @@ function greet(name: unknown) {
|
|
|
37
37
|
|
|
38
38
|
### `assert` functions
|
|
39
39
|
|
|
40
|
-
`assert` functions are assertions that throw a `TypeError` if the input value is not of the expected type. You can use them to ensure that a variable has a certain type before using it.
|
|
40
|
+
`assert` functions are assertions that throw a `TypeError` if the input value is not of the expected type. You can use them to ensure that a variable has a certain type before using it. You can also provide a custom error message as an optional second parameter.
|
|
41
41
|
|
|
42
42
|
```typescript
|
|
43
43
|
import { assertNumber } from "type-guardians/number";
|
|
@@ -54,9 +54,9 @@ function double(value: unknown) {
|
|
|
54
54
|
### String
|
|
55
55
|
|
|
56
56
|
- `isString(value: unknown): value is string`
|
|
57
|
-
- `assertString(value: unknown): asserts value is string`
|
|
57
|
+
- `assertString(value: unknown, message?: string): asserts value is string`
|
|
58
58
|
|
|
59
59
|
### Number
|
|
60
60
|
|
|
61
61
|
- `isNumber(value: unknown): value is number`
|
|
62
|
-
- `assertNumber(value: unknown): asserts value is number`
|
|
62
|
+
- `assertNumber(value: unknown, message?: string): asserts value is number`
|
package/number.ts
CHANGED
|
@@ -2,6 +2,9 @@ export function isNumber(value: unknown): value is string {
|
|
|
2
2
|
return typeof value === "number";
|
|
3
3
|
}
|
|
4
4
|
|
|
5
|
-
export function assertNumber(
|
|
6
|
-
|
|
5
|
+
export function assertNumber(
|
|
6
|
+
value: unknown,
|
|
7
|
+
message = `Expected a number, received ${typeof value}`,
|
|
8
|
+
): asserts value is number {
|
|
9
|
+
if (typeof value !== "number") throw new TypeError(message);
|
|
7
10
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "type-guardians",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"repository": "https://github.com/pchalupa/type-guardians",
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"type": "module",
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@biomejs/biome": "2.1.4",
|
|
16
|
+
"@vitest/coverage-v8": "3.1.2",
|
|
16
17
|
"typescript": "^5.9.2",
|
|
17
18
|
"vitest": "^3.1.2"
|
|
18
19
|
},
|
package/string.ts
CHANGED
|
@@ -2,6 +2,9 @@ export function isString(value: unknown): value is string {
|
|
|
2
2
|
return typeof value === "string";
|
|
3
3
|
}
|
|
4
4
|
|
|
5
|
-
export function assertString(
|
|
6
|
-
|
|
5
|
+
export function assertString(
|
|
6
|
+
value: unknown,
|
|
7
|
+
message = `Expected a string, received ${typeof value}`,
|
|
8
|
+
): asserts value is string {
|
|
9
|
+
if (typeof value !== "string") throw new TypeError(message);
|
|
7
10
|
}
|