Skip to content

Commit

Permalink
Option types
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleitnick committed Oct 17, 2023
1 parent ef39ddf commit d1a742e
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions modules/option/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
declare namespace Option {
interface Constructor {
readonly None: Option<never>;

Some: <T>(value: NonNullable<T>) => Option<T>;
Wrap: <T>(value: T) => Option<T>;
Is: (value: unknown) => boolean;
Assert: (value: unknown) => void;
Deserialize: <T>(data: SerializedOption<T>) => Option<T>;
}

interface SerializedOption<T> {
ClassName: "Option";
Value?: T;
}

interface Match<T, V> {
Some: (value: T) => V;
None: () => V;
}
}

interface Option<T> {
Serialize(): Option.SerializedOption<T>;
Match<V>(match: Option.Match<T, V>): V;
IsSome(): boolean;
IsNone(): boolean;
Expect(msg: string): T;
ExpectNone(msg: string): void;
Unwrap(): T;
UnwrapOr(defaultValue: T): T;
UnwrapOrElse(defaultFn: () => T): T;
And<O>(optionB: Option<O>): Option<O>;
AndThen<V>(andThenFn: (value: T) => V): Option<V>;
Or<O>(optionB: Option<O>): Option<T> | Option<O>;
OrElse<V>(orElseFn: (value: T) => V): Option<V>;
XOr<O>(optionB: Option<O>): Option<T> | Option<O>;
Filter(predicate: (value: T) => boolean): Option<T>;
Contains(value: T): boolean;
}

declare const Option: Option.Constructor;

export = Option;

0 comments on commit d1a742e

Please sign in to comment.