Skip to content

Commit 34770c0

Browse files
committed
💪 Improve docs/tests/types of as module
1 parent 68be2f1 commit 34770c0

File tree

8 files changed

+314
-218
lines changed

8 files changed

+314
-218
lines changed

_annotation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ export function hasAnnotation<F extends Fn, N extends string>(
3232
/**
3333
* Annotation for optional.
3434
*/
35-
export type WithOptional<T = unknown> = {
35+
export type AsOptional<T = unknown> = {
3636
optional: Predicate<T>;
3737
};
3838

3939
/**
4040
* Annotation for readonly.
4141
*/
42-
export type WithReadonly<T = unknown> = {
42+
export type AsReadonly<T = unknown> = {
4343
readonly: Predicate<T>;
4444
};
4545

as/__snapshots__/optional_test.ts.snap

Lines changed: 0 additions & 11 deletions
This file was deleted.

as/optional.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@ import { rewriteName } from "../_funcutil.ts";
22
import type { Predicate, PredicateType } from "../type.ts";
33
import {
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

Comments
 (0)