Skip to content

Commit

Permalink
fix(maybe): ensure maybe type parameter excludes null and undefined
Browse files Browse the repository at this point in the history
Maybe is intended to encapsulate null and undefined values. It shouldn't show Maybe<undefined |
number> even though that's technically true. Despite this, Maybe.of(undefined) will still return a
Maybe<undefined> but this is expected.
  • Loading branch information
williamareynolds committed Jan 5, 2020
1 parent 2ec9d34 commit dc0fd5c
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/instance/Maybe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export abstract class Maybe<A> implements Monad1<URI, A> {
* @param a
*/
static of<A>(a: A): Maybe<A> {
if (a === undefined || a === null) return new Nothing()
return new Just(a)
if (a === undefined || a === null) return new Nothing() as Maybe<NonNullable<A>>
return new Just(a) as Maybe<NonNullable<A>>
}

/**
Expand Down Expand Up @@ -82,15 +82,15 @@ class Nothing<A> extends Maybe<A> {
}

map<B>(f: (a: A) => B): Maybe<B> {
return new Nothing()
return new Nothing() as Maybe<NonNullable<B>>
}

ap<B>(fab: Maybe<(a: A) => B>): Maybe<B> {
return new Nothing()
return new Nothing() as Maybe<NonNullable<B>>
}

chain<B>(_: (a: A) => Maybe<B>): Maybe<B> {
return new Nothing()
return new Nothing() as Maybe<NonNullable<B>>
}
}

Expand All @@ -112,7 +112,7 @@ class Just<A> extends Maybe<A> implements Monad1<URI, A> {
ap<B>(fab: Maybe<(a: A) => B>): Maybe<B> {
switch (fab.tag) {
case 'Nothing':
return new Nothing()
return new Nothing() as Maybe<NonNullable<B>>
case 'Just':
return Maybe.of(fab.value(this.value))
}
Expand Down

0 comments on commit dc0fd5c

Please sign in to comment.