@@ -2,13 +2,21 @@ import { rewriteName } from "../_funcutil.ts";
22import type { Predicate , PredicateType } from "../type.ts" ;
33import {
44 annotate ,
5+ type AsOptional ,
56 hasAnnotation ,
67 unannotate ,
7- type WithOptional ,
88} from "../_annotation.ts" ;
99
1010/**
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.
1220 *
1321 * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost.
1422 *
@@ -29,16 +37,16 @@ export function asOptional<P extends Predicate<unknown>>(
2937) :
3038 & Extract < P , Predicate < PredicateType < P > > >
3139 & Predicate < PredicateType < P > | undefined >
32- & WithOptional < PredicateType < P > > {
40+ & AsOptional < PredicateType < P > > {
3341 if ( hasAnnotation ( pred , "optional" ) ) {
3442 return pred as
3543 & Extract < P , Predicate < PredicateType < P > > >
3644 & Predicate < PredicateType < P > | undefined >
37- & WithOptional < PredicateType < P > > ;
45+ & AsOptional < PredicateType < P > > ;
3846 }
3947 return rewriteName (
4048 annotate (
41- ( x : unknown ) => x === undefined || pred ( x ) ,
49+ ( x ) => x === undefined || pred ( x ) ,
4250 "optional" ,
4351 pred ,
4452 ) ,
@@ -47,11 +55,16 @@ export function asOptional<P extends Predicate<unknown>>(
4755 ) as unknown as
4856 & Extract < P , Predicate < PredicateType < P > > >
4957 & Predicate < PredicateType < P > | undefined >
50- & WithOptional < PredicateType < P > > ;
58+ & AsOptional < PredicateType < P > > ;
5159}
5260
5361/**
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.
5568 *
5669 * To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost.
5770 *
@@ -89,6 +102,6 @@ export function hasOptional<
89102 : never ,
90103> (
91104 pred : P ,
92- ) : pred is P & WithOptional < T > {
105+ ) : pred is P & AsOptional < T > {
93106 return hasAnnotation ( pred , "optional" ) ;
94107}
0 commit comments