Skip to content

Commit

Permalink
fix result type inference
Browse files Browse the repository at this point in the history
  • Loading branch information
volf52 committed Jun 12, 2023
1 parent c290a5d commit 53b1e45
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-flies-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@carbonteq/fp": patch
---

Fix type inference for Result methods
9 changes: 7 additions & 2 deletions src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ type CombinedResultErr<T extends Result<unknown, unknown>[]> = {
[K in keyof T]: UnwrapResult<T[K]>['err'];
}[number];

export type CombineResults<T extends Result<unknown, unknown>[]> = Result<
CombinedResultOk<T>,
CombinedResultErr<T>
>;

export class Result<T, E> {
static readonly UNIT_RESULT: UnitResult = new Result(UNIT, null as never);

Expand All @@ -46,11 +51,11 @@ export class Result<T, E> {
private readonly error: E | null,
) {}

static Ok<T, E>(val: T): Result<T, E> {
static Ok<T, E = never>(val: T): Result<T, E> {
return new Result(val, null) as Result<T, E>;
}

static Err<T, E>(err: E): Result<T, E> {
static Err<E, T = never>(err: E): Result<T, E> {
return new Result(null, err) as Result<T, E>;
}

Expand Down

0 comments on commit 53b1e45

Please sign in to comment.