Skip to content

Commit 1c4402b

Browse files
committed
feat(hf): asserts
1 parent d478760 commit 1c4402b

File tree

24 files changed

+309
-10
lines changed

24 files changed

+309
-10
lines changed

docs/en/v1/api/common/asserts.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
outline: [2, 3]
3+
description: "The asserts() function throws when a predicate fails and narrows the input type when it passes."
4+
prev:
5+
text: "isType"
6+
link: "/en/v1/api/common/isType"
7+
next:
8+
text: "instanceOf"
9+
link: "/en/v1/api/common/instanceOf"
10+
---
11+
12+
# asserts
13+
14+
The **`asserts()`** function throws when a predicate fails and narrows the input type when it passes.
15+
16+
## Interactive example
17+
18+
<MonacoTSEditor
19+
src="/examples/v1/api/common/asserts/tryout.doc.ts"
20+
majorVersion="v1"
21+
height="460px"
22+
/>
23+
24+
## Syntax
25+
26+
```typescript
27+
function asserts<
28+
GenericInput extends unknown,
29+
GenericPredicate extends GenericInput
30+
>(
31+
input: GenericInput,
32+
predicate: (input: GenericInput) => input is GenericPredicate
33+
): asserts input is GenericPredicate
34+
```
35+
36+
## Parameters
37+
38+
- `input`: The value to validate.
39+
- `predicate`: A type-guard predicate used to validate and narrow the input.
40+
41+
## Return value
42+
43+
Nothing. It throws an `AssertsError` when the predicate fails.
44+
45+
## See also
46+
47+
- [`isType`](/en/v1/api/common/isType) - Builds type guards based on runtime checks
48+
- [`instanceOf`](/en/v1/api/common/instanceOf) - Type guard using constructors

docs/en/v1/api/common/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ Compares to one or multiple literal values with support for type guards on primi
7373
### [isType](/en/v1/api/common/isType)
7474
Type guard based on `typeof`, `Array.isArray`, iterables, etc.
7575

76+
### [asserts](/en/v1/api/common/asserts)
77+
Throws when a predicate fails and narrows the input type when it passes.
78+
7679
### [instanceOf](/en/v1/api/common/instanceOf)
7780
Type guard based on one or more constructors (typed `instanceof`).
7881

docs/en/v1/api/common/instanceOf.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
outline: [2, 3]
33
description: "The instanceOf() function creates a type guard based on one or several constructors. It checks instanceof while keeping precise typing."
44
prev:
5-
text: "isType"
6-
link: "/en/v1/api/common/isType"
5+
text: "asserts"
6+
link: "/en/v1/api/common/asserts"
77
next:
88
text: "hasKinds"
99
link: "/en/v1/api/common/hasKinds"

docs/en/v1/api/common/isType.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ prev:
55
text: "equal"
66
link: "/en/v1/api/common/equal"
77
next:
8-
text: "instanceOf"
9-
link: "/en/v1/api/common/instanceOf"
8+
text: "asserts"
9+
link: "/en/v1/api/common/asserts"
1010
---
1111

1212
# isType
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { asserts, type ExpectType, isType } from "@duplojs/utils";
2+
3+
const value = "demo" as string | number;
4+
asserts(value, isType("string"));
5+
6+
type checkString = ExpectType<
7+
typeof value,
8+
string,
9+
"strict"
10+
>;
11+
12+
const maybeId = 42 as number | undefined;
13+
asserts(maybeId, isType("number"));
14+
15+
type checkNumber = ExpectType<
16+
typeof maybeId,
17+
number,
18+
"strict"
19+
>;
20+

docs/fr/v1/api/common/asserts.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
outline: [2, 3]
3+
description: "La fonction asserts() lance une erreur si le predicate echoue et affine le type si il passe."
4+
prev:
5+
text: "isType"
6+
link: "/fr/v1/api/common/isType"
7+
next:
8+
text: "instanceOf"
9+
link: "/fr/v1/api/common/instanceOf"
10+
---
11+
12+
# asserts
13+
14+
La **`asserts()`** fonction lance une erreur si le predicate echoue et affine le type si il passe.
15+
16+
## Exemple interactif
17+
18+
<MonacoTSEditor
19+
src="/examples/v1/api/common/asserts/tryout.doc.ts"
20+
majorVersion="v1"
21+
height="460px"
22+
/>
23+
24+
## Syntaxe
25+
26+
```typescript
27+
function asserts<
28+
GenericInput extends unknown,
29+
GenericPredicate extends GenericInput
30+
>(
31+
input: GenericInput,
32+
predicate: (input: GenericInput) => input is GenericPredicate
33+
): asserts input is GenericPredicate
34+
```
35+
36+
## Parametres
37+
38+
- `input` : La valeur a valider.
39+
- `predicate` : Un predicate type guard pour valider et affiner l'entree.
40+
41+
## Valeur de retour
42+
43+
Rien. Elle lance un `AssertsError` quand le predicate echoue.
44+
45+
## Voir aussi
46+
47+
- [`isType`](/fr/v1/api/common/isType) - Cree un type guard base sur des verifications runtime
48+
- [`instanceOf`](/fr/v1/api/common/instanceOf) - Type guard via des constructeurs

docs/fr/v1/api/common/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ Compare à une ou plusieurs valeurs littérales avec support des type guards sur
7373
### [isType](/fr/v1/api/common/isType)
7474
Type guard basé sur `typeof`, `Array.isArray`, itérables, etc.
7575

76+
### [asserts](/fr/v1/api/common/asserts)
77+
Lance une erreur si le predicate echoue et affine le type si il passe.
78+
7679
### [instanceOf](/fr/v1/api/common/instanceOf)
7780
Type guard basé sur un ou plusieurs constructeurs (`instanceof` typé).
7881

docs/fr/v1/api/common/instanceOf.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
outline: [2, 3]
33
description: "La fonction instanceOf() crée un type guard basé sur un ou plusieurs constructeurs. Elle vérifie instanceof en conservant un typage précis."
44
prev:
5-
text: "isType"
6-
link: "/fr/v1/api/common/isType"
5+
text: "asserts"
6+
link: "/fr/v1/api/common/asserts"
77
next:
88
text: "hasKinds"
99
link: "/fr/v1/api/common/hasKinds"

docs/fr/v1/api/common/isType.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ prev:
55
text: "equal"
66
link: "/fr/v1/api/common/equal"
77
next:
8-
text: "instanceOf"
9-
link: "/fr/v1/api/common/instanceOf"
8+
text: "asserts"
9+
link: "/fr/v1/api/common/asserts"
1010
---
1111

1212
# isType
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
var errorKindNamespace = require('./errorKindNamespace.cjs');
4+
var kind = require('./kind.cjs');
5+
6+
class AssertsError extends kind.kindHeritage("asserts-error", errorKindNamespace.createErrorKind("asserts-error"), Error) {
7+
value;
8+
constructor(value) {
9+
super({}, ["Asserts Error."]);
10+
this.value = value;
11+
}
12+
}
13+
/**
14+
* {@include common/asserts/index.md}
15+
*/
16+
function asserts(input, predicate) {
17+
if (!predicate(input)) {
18+
throw new AssertsError(input);
19+
}
20+
}
21+
22+
exports.AssertsError = AssertsError;
23+
exports.asserts = asserts;

0 commit comments

Comments
 (0)