Skip to content

Commit

Permalink
Add explicit types to some, none
Browse files Browse the repository at this point in the history
  • Loading branch information
bkiac committed Nov 15, 2023
1 parent ed0eb2e commit 97c1ec8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 47 deletions.
42 changes: 19 additions & 23 deletions src/option/none.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,83 +7,79 @@ export class NoneImpl implements OptionMethods<never> {
readonly none = true
readonly value = null

and<U>(_other: Option<U>) {
and<U>(_other: Option<U>): None {
return None
}

andThen<U>(_f: (value: never) => Option<U>) {
andThen<U>(_f: (value: never) => Option<U>): None {
return None
}

expect(panic: string): never {
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<U>(_f: (value: never) => U) {
map<U>(_f: (value: never) => U): None {
return None
}

mapOr<A, B>(defaultValue: A, _f: (value: never) => B) {
mapOr<A, B>(defaultValue: A, _f: (value: never) => B): A {
return defaultValue
}

mapOrElse<A, B>(defaultValue: () => A, _f: (value: never) => B) {
mapOrElse<A, B>(defaultValue: () => A, _f: (value: never) => B): A {
return defaultValue()
}

or<U>(other: Option<U>) {
or<U>(other: Option<U>): Option<U> {
return other
}

orElse<U>(f: () => Option<U>) {
orElse<U>(f: () => Option<U>): Option<U> {
return f()
}

unwrap(): never {
throw new UnwrapPanic("Cannot unwrap on a None")
throw new UnwrapPanic(`called "unwrap()" on None`)
}

unwrapOr<U>(defaultValue: U) {
unwrapOr<U>(defaultValue: U): U {
return defaultValue
}

unwrapOrElse<U>(defaultValue: () => U) {
unwrapOrElse<U>(defaultValue: () => U): U {
return defaultValue()
}

xor<U>(other: Option<U>) {
xor<U>(other: Option<U>): Option<U> {
return other
}

get() {
return null
}

match<A, B>(_some: (value: never) => A, none: () => B) {
match<A, B>(_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
}
}
Expand Down
44 changes: 20 additions & 24 deletions src/option/some.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,84 +11,80 @@ export class SomeImpl<T> implements OptionMethods<T> {
this.value = value
}

and<U>(other: Option<U>) {
and<U>(other: Option<U>): Option<U> {
return other
}

andThen<U>(f: (value: T) => Option<U>) {
andThen<U>(f: (value: T) => Option<U>): Option<U> {
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<T> {
return f(this.value) ? this : None
}

inspect(f: (value: T) => void) {
inspect(f: (value: T) => void): this {
f(this.value)
return this
}

map<U>(f: (value: T) => U) {
return Some(f(this.value))
map<U>(f: (value: T) => U): Some<U> {
return new SomeImpl(f(this.value))
}

mapOr<A, B>(_defaultValue: A, f: (value: T) => B) {
mapOr<A, B>(_defaultValue: A, f: (value: T) => B): B {
return f(this.value)
}

mapOrElse<A, B>(_defaultValue: () => A, f: (value: T) => B) {
mapOrElse<A, B>(_defaultValue: () => A, f: (value: T) => B): B {
return f(this.value)
}

or<U>(_other: Option<U>) {
or<U>(_other: Option<U>): this {
return this
}

orElse<U>(_f: () => Option<U>) {
orElse<U>(_f: () => Option<U>): this {
return this
}

unwrap() {
unwrap(): T {
return this.value
}

unwrapOr<U>(_defaultValue: U) {
unwrapOr<U>(_defaultValue: U): T {
return this.value
}

unwrapOrElse<U>(_defaultValue: () => U) {
unwrapOrElse<U>(_defaultValue: () => U): T {
return this.value
}

xor<U>(other: Option<U>) {
xor<U>(other: Option<U>): Option<T | U> {
return other.some ? None : this
}

get() {
return this.value
}

match<A, B>(some: (value: T) => A, _none: () => B) {
match<A, B>(some: (value: T) => A, _none: () => B): A {
return some(this.value)
}

toString() {
toString(): `Some(${string})` {
return `Some(${this.value})` as const
}

[inspectSymbol]() {
[inspectSymbol](): ReturnType<OptionMethods<T>["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
}
}
Expand Down

0 comments on commit 97c1ec8

Please sign in to comment.