summaryrefslogtreecommitdiff
path: root/shared/utils/src/optional.ts
diff options
context:
space:
mode:
Diffstat (limited to 'shared/utils/src/optional.ts')
-rw-r--r--shared/utils/src/optional.ts22
1 files changed, 22 insertions, 0 deletions
diff --git a/shared/utils/src/optional.ts b/shared/utils/src/optional.ts
new file mode 100644
index 0000000..7058803
--- /dev/null
+++ b/shared/utils/src/optional.ts
@@ -0,0 +1,22 @@
+export type Optional<T> = T | None;
+export type None = null | undefined;
+
+/**
+ * Determine if an optional value is present.
+ *
+ * @param optional value
+ * @return true if present, false otherwise
+ */
+export function isSome<T>(optional: Optional<T>): optional is T {
+ return optional !== null && optional !== undefined;
+}
+
+/**
+ * Determine if an optional value is not present.
+ *
+ * @param optional value
+ * @return true if not present, false otherwise
+ */
+export function isNone<T>(optional: Optional<T>): optional is None {
+ return optional === null || optional === undefined;
+}