Skip to content

Commit

Permalink
fix(swap types): make swap types more generic
Browse files Browse the repository at this point in the history
Swap's types was keeping TypeScript from comparing some tasks. This makes the generics even more
generic.
  • Loading branch information
tdreyno committed Feb 18, 2020
1 parent 0049a1f commit 8c36397
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/Task/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ export class Task<E, S> implements PromiseLike<S> {

public fork: Fork<E, S>;

// eslint-disable-next-line @typescript-eslint/unbound-method
public andThen = this.chain;

constructor(computation: Fork<E, S>) {
this.fork = computation;
}
Expand Down Expand Up @@ -78,8 +75,8 @@ export class Task<E, S> implements PromiseLike<S> {
return toPromise(this);
}

public swap(): Task<S, E> {
return swap(this);
public swap<E2 extends E, S2 extends S>(): Task<S2, E2> {
return swap<E, S, E2, S2>(this);
}

public map<S2>(fn: (result: S) => S2): Task<E, S2> {
Expand Down Expand Up @@ -300,7 +297,6 @@ export function fork<E, S>(

/**
* Chain a task to run after a previous task has succeeded.
* @alias andThen
* @param fn Takes a successful result and returns a new task.
* @param task The task which will chain to the next one on success.
*/
Expand All @@ -313,8 +309,6 @@ export function chain<E, S, S2>(
);
}

export const andThen = chain;

/**
* If a function returns a Promise instead of a Task, automatically
* convert it to a Task.
Expand Down Expand Up @@ -700,8 +694,15 @@ export function sequence<E, S>(
* Given a task, swap the error and success values.
* @param task The task to swap the results of.
*/
export function swap<E, S>(task: Task<E, S>): Task<S, E> {
return new Task<S, E>((reject, resolve) => task.fork(resolve, reject));
export function swap<E, S, E2 extends E, S2 extends S>(
task: Task<E, S>
): Task<S2, E2> {
return new Task<S2, E2>((reject, resolve) =>
task.fork(
e => resolve(e as E2),
s => reject(s as S2)
)
);
}

/**
Expand Down

0 comments on commit 8c36397

Please sign in to comment.