Skip to content

Commit

Permalink
feat(api): add deref(), isDeref() fns & MaybeDeref
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed May 31, 2020
1 parent fd141c5 commit 2ab46ad
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/api/src/api/deref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface IDeref<T> {
deref(): T;
}

export type MaybeDeref<T> = IDeref<T> | T;

/**
* If `T` is a {@link IDeref}, returns its value type or else `T`.
*/
Expand Down Expand Up @@ -40,3 +42,19 @@ export type DerefedKeys<
> = {
[P in K]: Derefed<T[P]>;
};

/**
* Returns true iff `x` implements {@link IDeref}.
*
* @param x
*/
export const isDeref = (x: any): x is IDeref<any> =>
x != null && typeof x["deref"] === "function";

/**
* If `x` implements {@link IDeref}, returns its wrapped value, else
* returns `x` itself.
*
* @param x -
*/
export const deref = <T>(x: MaybeDeref<T>) => (isDeref(x) ? x.deref() : x);

0 comments on commit 2ab46ad

Please sign in to comment.