diff --git a/src/runtime/type-guards/isThenable.ts b/src/runtime/type-guards/isThenable.ts new file mode 100644 index 00000000..c98aa876 --- /dev/null +++ b/src/runtime/type-guards/isThenable.ts @@ -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"; +} diff --git a/src/types/base-types/Thenable.ts b/src/types/base-types/Thenable.ts new file mode 100644 index 00000000..2028ff70 --- /dev/null +++ b/src/types/base-types/Thenable.ts @@ -0,0 +1,12 @@ +import { Dictionary } from "./Dictionary"; + +/** + * **Thenable**`` + * + * An object which resembles a promise and is guarenteed to provide a `.then` and `.catch` + * callback hook. + */ +export type Thenable = Dictionary & { + then: (cb: T) => void; + catch: (err: string) => void; +} diff --git a/src/types/base-types/index.ts b/src/types/base-types/index.ts index ff50eaac..c6fc936e 100644 --- a/src/types/base-types/index.ts +++ b/src/types/base-types/index.ts @@ -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";