Skip to content

Commit

Permalink
fix: use single type parameter in match methods of Async and Maybe
Browse files Browse the repository at this point in the history
  • Loading branch information
faergeek committed Feb 8, 2024
1 parent be9f813 commit d34e7b7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/async.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ describe('Async', () => {
});

it('#mapReady', () => {
expectTypeOf(Async.Pending.mapReady(() => 42)).toEqualTypeOf<
Async<number>
>();

const matched = Async.Pending.mapReady(() => 42).match({
Pending: () => 0,
Ready: value => value,
Expand All @@ -34,6 +38,12 @@ describe('Async', () => {
});

it('#flatMapReady', () => {
expectTypeOf(
Async.Ready('anything').flatMapReady(s =>
s === 'something' ? Async.Ready(s) : Async.Pending,
),
).toEqualTypeOf<Async<string>>();

const matched = Async.Ready('anything')
.flatMapReady(s => (s === 'something' ? Async.Ready(s) : Async.Pending))
.match({
Expand Down
2 changes: 1 addition & 1 deletion src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class Async<T> {
* values.
* This is used to implement most of the operations
*/
match<U, P>(arms: { Pending: () => P; Ready: (value: T) => U }) {
match<U>(arms: { Pending: () => U; Ready: (value: T) => U }) {
return this.#state.isReady ? arms.Ready(this.#state.value) : arms.Pending();
}

Expand Down
8 changes: 8 additions & 0 deletions src/maybe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ describe('Maybe', () => {
});

it('#mapSome', () => {
expectTypeOf(Maybe.None.mapSome(() => 42)).toEqualTypeOf<Maybe<number>>();

const matched = Maybe.None.mapSome(() => 42).match({
None: () => 0,
Some: value => value,
Expand All @@ -34,6 +36,12 @@ describe('Maybe', () => {
});

it('#flatMapSome', () => {
expectTypeOf(
Maybe.None.flatMapSome(s =>
s === 'something' ? Maybe.None : Maybe.Some('asdfasdf'),
),
).toEqualTypeOf<Maybe<string>>();

const matched = Maybe.Some('anything')
.flatMapSome(s => (s === 'something' ? Maybe.Some(s) : Maybe.None))
.match({
Expand Down
2 changes: 1 addition & 1 deletion src/maybe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class Maybe<T> {
* value.
* This is used to implement most of the operations
*/
match<U, N>(arms: { None: () => N; Some: (value: T) => U }) {
match<U>(arms: { None: () => U; Some: (value: T) => U }) {
return this.#state.isSome ? arms.Some(this.#state.value) : arms.None();
}

Expand Down

0 comments on commit d34e7b7

Please sign in to comment.