From e04efc54764b106d428ab495b18d1bf1d9a8c3ea Mon Sep 17 00:00:00 2001 From: Ken Snyder Date: Wed, 11 Sep 2024 13:36:28 -0700 Subject: [PATCH] feat: added Thenable type and isThenable() type guard --- src/runtime/type-guards/isThenable.ts | 8 ++++++++ src/types/base-types/Thenable.ts | 12 ++++++++++++ src/types/base-types/index.ts | 1 + 3 files changed, 21 insertions(+) create mode 100644 src/runtime/type-guards/isThenable.ts create mode 100644 src/types/base-types/Thenable.ts 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";