Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/docs/option.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,22 @@ if (option.isSome()) {
}
```

### .getOrThrow()

```ts
Option<A>.getOrThrow(): A | never
```

Returns the value contained in `Some(value)`, otherwise will throw an error.

```ts title="Examples"
const value = Option.Some(1).getOrThrow();
// 2

const value = Option.None().getOrThrow();
// Uncaught: Tried to unwrap a Option.None value
```

### .isSome()

```ts
Expand Down
16 changes: 16 additions & 0 deletions docs/docs/result.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,22 @@ Result.Error(2).getOr(1);
// 1
```

### .getOrThrow()

```ts
Result<A, E>.getOrThrow(): A | never
```

If the result is `Ok(value)` returns `value`, otherwise will throw an error.

```ts title="Examples"
const value = Result.Ok(1).getOrThrow();
// 2

const value = Result.Error(2).getOrThrow();
// Uncaught: Tried to unwrap a Result.Error value
```

### .mapOr(defaultValue, mapper)

```ts
Expand Down
10 changes: 10 additions & 0 deletions src/AsyncData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,16 @@ class __AsyncData<A> {
return (this as Done<A>).value;
}

/**
* Return the loaded value if present, otherwise throw an error
*/
getOrThrow(this: AsyncData<A>): A | never {
if (this === NOT_ASKED || this === LOADING) {
throw new Error("Tried to unwrap an incomplete AsyncData");
}
return (this as Done<A>).value;
}

/**
* Maps the value if present, returns the fallback otherwise
*
Expand Down
26 changes: 26 additions & 0 deletions src/OptionResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,17 @@ class __Option<A> {
return (this as Some<A>).value;
}

/**
* Return the value if present, otherwise throw an error
*/
getOrThrow(this: Option<A>): A | never {
if (this === NONE) {
throw new Error("Tried to unwrap a Option.None value");
}

return (this as Some<A>).value;
}

/**
* Return the value if present, and the fallback otherwise
*
Expand Down Expand Up @@ -589,6 +600,21 @@ class __Result<A, E> {
return this.tag === "Ok" ? this.value : defaultValue;
}

/**
* Return the value if present, otherwise throw an error
*/
getOrThrow(this: Result<A, E>): A | never {
if (this.tag === "Error") {
if (typeof this.error === "string") {
throw new Error(this.error);
}

throw new Error("Tried to unwrap a Result.Error value");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd throw this.error directly in this case, or at least put it in the error's cause.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea, I wanted to use cause, unfortunately it is not supported yet by the Typescript version used in the project.

Now throwing this.error if the error is string. Also added the implementation for AsyncData

}

return this.value;
}

/**
* Maps the value if present, returns the fallback otherwise
*
Expand Down
10 changes: 10 additions & 0 deletions test/AsyncData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ test("AsyncData.getOr", () => {
expect(AsyncData.Done(5).getOr(0)).toEqual(5);
});

test("AsyncData.getOrThrow", () => {
expect(AsyncData.Done(5).getOrThrow()).toEqual(5);
expect(() => AsyncData.Loading<number>().getOrThrow()).toThrowError(
"Tried to unwrap an incomplete AsyncData",
);
expect(() => AsyncData.NotAsked<number>().getOrThrow()).toThrowError(
"Tried to unwrap an incomplete AsyncData",
);
});

test("AsyncData.mapOk", () => {
expect(AsyncData.NotAsked<number>().mapOr(0, (x) => x * 2)).toEqual(0);
expect(AsyncData.Loading<number>().mapOr(0, (x) => x * 2)).toEqual(0);
Expand Down
7 changes: 7 additions & 0 deletions test/Option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ test("Option.getOr", () => {
expect(Option.None<number>().getOr(0)).toEqual(0);
});

test("Option.getOrThrow", () => {
expect(Option.Some(1).getOrThrow()).toEqual(1);
expect(() => Option.None().getOrThrow()).toThrowError(
"Tried to unwrap a Option.None value",
);
});

test("Option.mapOr", () => {
expect(Option.Some(1).mapOr(0, (x) => x * 2)).toEqual(2);
expect(Option.None<number>().mapOr(0, (x) => x * 2)).toEqual(0);
Expand Down
10 changes: 10 additions & 0 deletions test/Result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ test("Result.getOr", () => {
expect(Result.Error<number, number>(1).getOr(0)).toEqual(0);
});

test("Result.getOrThrow", () => {
expect(Result.Ok(1).getOrThrow()).toEqual(1);
expect(() => Result.Error(1).getOrThrow()).toThrowError(
"Tried to unwrap a Result.Error value",
);
expect(() => Result.Error("AnErrorMessage").getOrThrow()).toThrowError(
"AnErrorMessage",
);
});

test("Result.mapOr", () => {
expect(Result.Ok(1).mapOr(0, (x) => x * 2)).toEqual(2);
expect(Result.Error<number, number>(1).mapOr(0, (x) => x * 2)).toEqual(0);
Expand Down