Skip to content

Commit 8c36397

Browse files
committed
fix(swap types): make swap types more generic
Swap's types was keeping TypeScript from comparing some tasks. This makes the generics even more generic.
1 parent 0049a1f commit 8c36397

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/Task/Task.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ export class Task<E, S> implements PromiseLike<S> {
3838

3939
public fork: Fork<E, S>;
4040

41-
// eslint-disable-next-line @typescript-eslint/unbound-method
42-
public andThen = this.chain;
43-
4441
constructor(computation: Fork<E, S>) {
4542
this.fork = computation;
4643
}
@@ -78,8 +75,8 @@ export class Task<E, S> implements PromiseLike<S> {
7875
return toPromise(this);
7976
}
8077

81-
public swap(): Task<S, E> {
82-
return swap(this);
78+
public swap<E2 extends E, S2 extends S>(): Task<S2, E2> {
79+
return swap<E, S, E2, S2>(this);
8380
}
8481

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

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

316-
export const andThen = chain;
317-
318312
/**
319313
* If a function returns a Promise instead of a Task, automatically
320314
* convert it to a Task.
@@ -700,8 +694,15 @@ export function sequence<E, S>(
700694
* Given a task, swap the error and success values.
701695
* @param task The task to swap the results of.
702696
*/
703-
export function swap<E, S>(task: Task<E, S>): Task<S, E> {
704-
return new Task<S, E>((reject, resolve) => task.fork(resolve, reject));
697+
export function swap<E, S, E2 extends E, S2 extends S>(
698+
task: Task<E, S>
699+
): Task<S2, E2> {
700+
return new Task<S2, E2>((reject, resolve) =>
701+
task.fork(
702+
e => resolve(e as E2),
703+
s => reject(s as S2)
704+
)
705+
);
705706
}
706707

707708
/**

0 commit comments

Comments
 (0)