@@ -2,13 +2,21 @@ import { rewriteName } from "../_funcutil.ts";
2
2
import type { Predicate , PredicateType } from "../type.ts" ;
3
3
import {
4
4
annotate ,
5
+ type AsOptional ,
5
6
hasAnnotation ,
6
7
unannotate ,
7
- type WithOptional ,
8
8
} from "../_annotation.ts" ;
9
9
10
10
/**
11
- * Return an `Optional` annotated type predicate function that returns `true` if the type of `x` is `T` or `undefined`.
11
+ * Annotate the given predicate function as optional.
12
+ *
13
+ * Use this function to annotate a predicate function of `predObj` in {@linkcode isObjectOf}.
14
+ *
15
+ * Note that the annotated predicate function will return `true` if the type of `x` is `T` or `undefined`, indicating that
16
+ * this function is not just for annotation but it also changes the behavior of the predicate function.
17
+ *
18
+ * Use {@linkcode asUnoptional} to remove the annotation.
19
+ * Use {@linkcode hasOptional} to check if a predicate function has annotated with this function.
12
20
*
13
21
* To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost.
14
22
*
@@ -29,16 +37,16 @@ export function asOptional<P extends Predicate<unknown>>(
29
37
) :
30
38
& Extract < P , Predicate < PredicateType < P > > >
31
39
& Predicate < PredicateType < P > | undefined >
32
- & WithOptional < PredicateType < P > > {
40
+ & AsOptional < PredicateType < P > > {
33
41
if ( hasAnnotation ( pred , "optional" ) ) {
34
42
return pred as
35
43
& Extract < P , Predicate < PredicateType < P > > >
36
44
& Predicate < PredicateType < P > | undefined >
37
- & WithOptional < PredicateType < P > > ;
45
+ & AsOptional < PredicateType < P > > ;
38
46
}
39
47
return rewriteName (
40
48
annotate (
41
- ( x : unknown ) => x === undefined || pred ( x ) ,
49
+ ( x ) => x === undefined || pred ( x ) ,
42
50
"optional" ,
43
51
pred ,
44
52
) ,
@@ -47,11 +55,16 @@ export function asOptional<P extends Predicate<unknown>>(
47
55
) as unknown as
48
56
& Extract < P , Predicate < PredicateType < P > > >
49
57
& Predicate < PredicateType < P > | undefined >
50
- & WithOptional < PredicateType < P > > ;
58
+ & AsOptional < PredicateType < P > > ;
51
59
}
52
60
53
61
/**
54
- * Return an `Optional` un-annotated type predicate function that returns `true` if the type of `x` is `T`.
62
+ * Unannotate the annotated predicate function with {@linkcode asOptional}.
63
+ *
64
+ * Use this function to unannotate a predicate function of `predObj` in {@linkcode isObjectOf}.
65
+ *
66
+ * Note that the annotated predicate function will return `true` if the type of `x` is `T`, indicating that
67
+ * this function is not just for annotation but it also changes the behavior of the predicate function.
55
68
*
56
69
* To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost.
57
70
*
@@ -89,6 +102,6 @@ export function hasOptional<
89
102
: never ,
90
103
> (
91
104
pred : P ,
92
- ) : pred is P & WithOptional < T > {
105
+ ) : pred is P & AsOptional < T > {
93
106
return hasAnnotation ( pred , "optional" ) ;
94
107
}
0 commit comments