From 97c1ec802f64a89b76d9aedf66428688252e305d Mon Sep 17 00:00:00 2001 From: bkiac Date: Wed, 15 Nov 2023 20:31:45 +0100 Subject: [PATCH] Add explicit types to some, none --- src/option/none.ts | 42 +++++++++++++++++++----------------------- src/option/some.ts | 44 ++++++++++++++++++++------------------------ 2 files changed, 39 insertions(+), 47 deletions(-) diff --git a/src/option/none.ts b/src/option/none.ts index 55f0f10..5945471 100644 --- a/src/option/none.ts +++ b/src/option/none.ts @@ -7,11 +7,11 @@ export class NoneImpl implements OptionMethods { readonly none = true readonly value = null - and(_other: Option) { + and(_other: Option): None { return None } - andThen(_f: (value: never) => Option) { + andThen(_f: (value: never) => Option): None { return None } @@ -19,71 +19,67 @@ export class NoneImpl implements OptionMethods { throw new Panic(panic) } - filter(_f: (value: never) => boolean) { + filter(_f: (value: never) => boolean): None { return None } - inspect(_f: (value: never) => void) { - return this + inspect(_f: (value: never) => void): None { + return None } - map(_f: (value: never) => U) { + map(_f: (value: never) => U): None { return None } - mapOr(defaultValue: A, _f: (value: never) => B) { + mapOr(defaultValue: A, _f: (value: never) => B): A { return defaultValue } - mapOrElse(defaultValue: () => A, _f: (value: never) => B) { + mapOrElse(defaultValue: () => A, _f: (value: never) => B): A { return defaultValue() } - or(other: Option) { + or(other: Option): Option { return other } - orElse(f: () => Option) { + orElse(f: () => Option): Option { return f() } unwrap(): never { - throw new UnwrapPanic("Cannot unwrap on a None") + throw new UnwrapPanic(`called "unwrap()" on None`) } - unwrapOr(defaultValue: U) { + unwrapOr(defaultValue: U): U { return defaultValue } - unwrapOrElse(defaultValue: () => U) { + unwrapOrElse(defaultValue: () => U): U { return defaultValue() } - xor(other: Option) { + xor(other: Option): Option { return other } - get() { - return null - } - - match(_some: (value: never) => A, none: () => B) { + match(_some: (value: never) => A, none: () => B): B { return none() } - toString() { + toString(): "None" { return "None" as const } - [inspectSymbol]() { + [inspectSymbol](): "None" { return this.toString() } - toObject() { + toObject(): {some: false; value: null} { return {some: false, value: null} as const } - toJSON() { + toJSON(): {meta: "None"; value: null} { return {meta: "None", value: null} as const } } diff --git a/src/option/some.ts b/src/option/some.ts index dc3c598..20f58f9 100644 --- a/src/option/some.ts +++ b/src/option/some.ts @@ -11,84 +11,80 @@ export class SomeImpl implements OptionMethods { this.value = value } - and(other: Option) { + and(other: Option): Option { return other } - andThen(f: (value: T) => Option) { + andThen(f: (value: T) => Option): Option { return f(this.value) } - expect(_panic: string) { + expect(_panic: string): T { return this.value } - filter(f: (value: T) => boolean) { + filter(f: (value: T) => boolean): Option { return f(this.value) ? this : None } - inspect(f: (value: T) => void) { + inspect(f: (value: T) => void): this { f(this.value) return this } - map(f: (value: T) => U) { - return Some(f(this.value)) + map(f: (value: T) => U): Some { + return new SomeImpl(f(this.value)) } - mapOr(_defaultValue: A, f: (value: T) => B) { + mapOr(_defaultValue: A, f: (value: T) => B): B { return f(this.value) } - mapOrElse(_defaultValue: () => A, f: (value: T) => B) { + mapOrElse(_defaultValue: () => A, f: (value: T) => B): B { return f(this.value) } - or(_other: Option) { + or(_other: Option): this { return this } - orElse(_f: () => Option) { + orElse(_f: () => Option): this { return this } - unwrap() { + unwrap(): T { return this.value } - unwrapOr(_defaultValue: U) { + unwrapOr(_defaultValue: U): T { return this.value } - unwrapOrElse(_defaultValue: () => U) { + unwrapOrElse(_defaultValue: () => U): T { return this.value } - xor(other: Option) { + xor(other: Option): Option { return other.some ? None : this } - get() { - return this.value - } - - match(some: (value: T) => A, _none: () => B) { + match(some: (value: T) => A, _none: () => B): A { return some(this.value) } - toString() { + toString(): `Some(${string})` { return `Some(${this.value})` as const } - [inspectSymbol]() { + [inspectSymbol](): ReturnType["toString"]> { return this.toString() } - toObject() { + toObject(): {some: true; value: T} { return {some: true, value: this.value} as const } - toJSON() { + toJSON(): {meta: "Some"; value: T} { return {meta: "Some", value: this.value} as const } }