From d1a742edc74eb8e90a1e777b3271246d3ecd1487 Mon Sep 17 00:00:00 2001 From: Stephen Leitnick Date: Tue, 17 Oct 2023 11:49:35 -0400 Subject: [PATCH] Option types --- modules/option/index.d.ts | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 modules/option/index.d.ts diff --git a/modules/option/index.d.ts b/modules/option/index.d.ts new file mode 100644 index 00000000..a7700aa4 --- /dev/null +++ b/modules/option/index.d.ts @@ -0,0 +1,44 @@ +declare namespace Option { + interface Constructor { + readonly None: Option; + + Some: (value: NonNullable) => Option; + Wrap: (value: T) => Option; + Is: (value: unknown) => boolean; + Assert: (value: unknown) => void; + Deserialize: (data: SerializedOption) => Option; + } + + interface SerializedOption { + ClassName: "Option"; + Value?: T; + } + + interface Match { + Some: (value: T) => V; + None: () => V; + } +} + +interface Option { + Serialize(): Option.SerializedOption; + Match(match: Option.Match): V; + IsSome(): boolean; + IsNone(): boolean; + Expect(msg: string): T; + ExpectNone(msg: string): void; + Unwrap(): T; + UnwrapOr(defaultValue: T): T; + UnwrapOrElse(defaultFn: () => T): T; + And(optionB: Option): Option; + AndThen(andThenFn: (value: T) => V): Option; + Or(optionB: Option): Option | Option; + OrElse(orElseFn: (value: T) => V): Option; + XOr(optionB: Option): Option | Option; + Filter(predicate: (value: T) => boolean): Option; + Contains(value: T): boolean; +} + +declare const Option: Option.Constructor; + +export = Option;