Skip to content

Commit

Permalink
docs: add documentations of is/as
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed Aug 9, 2024
1 parent 6945b17 commit 66b2317
Show file tree
Hide file tree
Showing 2 changed files with 979 additions and 11 deletions.
105 changes: 99 additions & 6 deletions as/mod.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,103 @@
import { asOptional, asUnoptional } from "./optional.ts";
import { asReadonly, asUnreadonly } from "./readonly.ts";
// NOTE: This file is generated by gen-mod.ts
import { asOptional } from "./optional.ts";
import { asReadonly } from "./readonly.ts";
import { asUnoptional } from "./optional.ts";
import { asUnreadonly } from "./readonly.ts";

/**
* Annotation collection for object predicate properties.
*/
export const as = {
export const as: {
/**
* Annotate the given predicate function as optional.
*
* Use this function to annotate a predicate function of `predObj` in {@linkcode isObjectOf}.
*
* Note that the annotated predicate function will return `true` if the type of `x` is `T` or `undefined`, indicating that
* this function is not just for annotation but it also changes the behavior of the predicate function.
*
* Use {@linkcode asUnoptional} to remove the annotation.
* Use {@linkcode hasOptional} to check if a predicate function has annotated with this function.
*
* To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost.
*
* ```ts
* import { as, is } from "@core/unknownutil";
*
* const isMyType = is.ObjectOf({
* foo: as.Optional(is.String),
* });
* const a: unknown = {};
* if (isMyType(a)) {
* const _: {foo?: string} = a;
* }
* ```
*/
Optional: typeof asOptional;
/**
* Annotate the given predicate function as readonly.
*
* Use this function to annotate a predicate function of `predObj` in {@linkcode isObjectOf}.
*
* Use {@linkcode asUnreadonly} to remove the annotation.
* Use {@linkcode hasReadonly} to check if a predicate function has annotated with this function.
*
* To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost.
*
* ```ts
* import { as, is } from "@core/unknownutil";
*
* const isMyType = is.ObjectOf({
* foo: as.Readonly(is.String),
* });
* const a: unknown = {};
* if (isMyType(a)) {
* const _: {readonly foo: string} = a;
* }
* ```
*/
Readonly: typeof asReadonly;
/**
* Unannotate the annotated predicate function with {@linkcode asOptional}.
*
* Use this function to unannotate a predicate function of `predObj` in {@linkcode isObjectOf}.
*
* Note that the annotated predicate function will return `true` if the type of `x` is `T`, indicating that
* this function is not just for annotation but it also changes the behavior of the predicate function.
*
* To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost.
*
* ```ts
* import { as, is } from "@core/unknownutil";
*
* const isMyType = is.ObjectOf({
* foo: as.Unoptional(as.Optional(is.String)),
* });
* const a: unknown = {foo: "a"};
* if (isMyType(a)) {
* const _: {foo: string} = a;
* }
* ```
*/
Unoptional: typeof asUnoptional;
/**
* Unannotate the annotated predicate function with {@linkcode asReadonly}.
*
* Use this function to unannotate a predicate function of `predObj` in {@linkcode isObjectOf}.
*
* To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost.
*
* ```ts
* import { as, is } from "@core/unknownutil";
*
* const isMyType = is.ObjectOf({
* foo: as.Unreadonly(as.Readonly(is.String)),
* });
* const a: unknown = {foo: "a"};
* if (isMyType(a)) {
* const _: {foo: string} = a;
* }
* ```
*/
Unreadonly: typeof asUnreadonly;
} = {
Optional: asOptional,
Readonly: asReadonly,
Unoptional: asUnoptional,
Expand Down
Loading

0 comments on commit 66b2317

Please sign in to comment.