Skip to content

Commit

Permalink
feat: added Thenable type and isThenable() type guard
Browse files Browse the repository at this point in the history
  • Loading branch information
yankeeinlondon committed Sep 11, 2024
1 parent 4f7279d commit e04efc5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/runtime/type-guards/isThenable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Thenable } from "src/types/index";
import { isObject } from "./isObject";
/**
* type guard which checks whether passed in `val` is a `Thenable` object
*/
export const isThenable = (val: unknown): val is Thenable => {
return isObject(val) && "then" in val && "catch" in val && typeof val.then === "function";
}
12 changes: 12 additions & 0 deletions src/types/base-types/Thenable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Dictionary } from "./Dictionary";

/**
* **Thenable**`<T>`
*
* An object which resembles a promise and is guarenteed to provide a `.then` and `.catch`
* callback hook.
*/
export type Thenable<T = unknown> = Dictionary & {
then: (cb: T) => void;
catch: (err: string) => void;
}
1 change: 1 addition & 0 deletions src/types/base-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from "./Scalar";
export * from "./ScalarNotSymbol";
export * from "./Container";
export * from "./Tuple";
export * from "./Thenable";
export * from "./NumericSign";
export * from "./Digital";
export * from "./Indexable";
Expand Down

0 comments on commit e04efc5

Please sign in to comment.