Skip to content

Commit 66b2317

Browse files
committed
docs: add documentations of is/as
1 parent 6945b17 commit 66b2317

File tree

2 files changed

+979
-11
lines changed

2 files changed

+979
-11
lines changed

as/mod.ts

Lines changed: 99 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,103 @@
1-
import { asOptional, asUnoptional } from "./optional.ts";
2-
import { asReadonly, asUnreadonly } from "./readonly.ts";
1+
// NOTE: This file is generated by gen-mod.ts
2+
import { asOptional } from "./optional.ts";
3+
import { asReadonly } from "./readonly.ts";
4+
import { asUnoptional } from "./optional.ts";
5+
import { asUnreadonly } from "./readonly.ts";
36

4-
/**
5-
* Annotation collection for object predicate properties.
6-
*/
7-
export const as = {
7+
export const as: {
8+
/**
9+
* Annotate the given predicate function as optional.
10+
*
11+
* Use this function to annotate a predicate function of `predObj` in {@linkcode isObjectOf}.
12+
*
13+
* Note that the annotated predicate function will return `true` if the type of `x` is `T` or `undefined`, indicating that
14+
* this function is not just for annotation but it also changes the behavior of the predicate function.
15+
*
16+
* Use {@linkcode asUnoptional} to remove the annotation.
17+
* Use {@linkcode hasOptional} to check if a predicate function has annotated with this function.
18+
*
19+
* To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost.
20+
*
21+
* ```ts
22+
* import { as, is } from "@core/unknownutil";
23+
*
24+
* const isMyType = is.ObjectOf({
25+
* foo: as.Optional(is.String),
26+
* });
27+
* const a: unknown = {};
28+
* if (isMyType(a)) {
29+
* const _: {foo?: string} = a;
30+
* }
31+
* ```
32+
*/
33+
Optional: typeof asOptional;
34+
/**
35+
* Annotate the given predicate function as readonly.
36+
*
37+
* Use this function to annotate a predicate function of `predObj` in {@linkcode isObjectOf}.
38+
*
39+
* Use {@linkcode asUnreadonly} to remove the annotation.
40+
* Use {@linkcode hasReadonly} to check if a predicate function has annotated with this function.
41+
*
42+
* To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost.
43+
*
44+
* ```ts
45+
* import { as, is } from "@core/unknownutil";
46+
*
47+
* const isMyType = is.ObjectOf({
48+
* foo: as.Readonly(is.String),
49+
* });
50+
* const a: unknown = {};
51+
* if (isMyType(a)) {
52+
* const _: {readonly foo: string} = a;
53+
* }
54+
* ```
55+
*/
56+
Readonly: typeof asReadonly;
57+
/**
58+
* Unannotate the annotated predicate function with {@linkcode asOptional}.
59+
*
60+
* Use this function to unannotate a predicate function of `predObj` in {@linkcode isObjectOf}.
61+
*
62+
* Note that the annotated predicate function will return `true` if the type of `x` is `T`, indicating that
63+
* this function is not just for annotation but it also changes the behavior of the predicate function.
64+
*
65+
* To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost.
66+
*
67+
* ```ts
68+
* import { as, is } from "@core/unknownutil";
69+
*
70+
* const isMyType = is.ObjectOf({
71+
* foo: as.Unoptional(as.Optional(is.String)),
72+
* });
73+
* const a: unknown = {foo: "a"};
74+
* if (isMyType(a)) {
75+
* const _: {foo: string} = a;
76+
* }
77+
* ```
78+
*/
79+
Unoptional: typeof asUnoptional;
80+
/**
81+
* Unannotate the annotated predicate function with {@linkcode asReadonly}.
82+
*
83+
* Use this function to unannotate a predicate function of `predObj` in {@linkcode isObjectOf}.
84+
*
85+
* To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost.
86+
*
87+
* ```ts
88+
* import { as, is } from "@core/unknownutil";
89+
*
90+
* const isMyType = is.ObjectOf({
91+
* foo: as.Unreadonly(as.Readonly(is.String)),
92+
* });
93+
* const a: unknown = {foo: "a"};
94+
* if (isMyType(a)) {
95+
* const _: {foo: string} = a;
96+
* }
97+
* ```
98+
*/
99+
Unreadonly: typeof asUnreadonly;
100+
} = {
8101
Optional: asOptional,
9102
Readonly: asReadonly,
10103
Unoptional: asUnoptional,

0 commit comments

Comments
 (0)