Skip to content

Commit 7b60140

Browse files
committed
fix orElseThrow does not accept a function
1 parent 2934040 commit 7b60140

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lassepitkanen/optionalts",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "TypeScript library that provides an Optional<T> type similar to Java's Optional class.",
55
"author": "Lasse Pitkänen",
66
"license": "MIT",

src/optional.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ describe('Optional', () => {
132132
describe('orElseThrow', () => {
133133
it('should return the value of a Some', () => {
134134
const optional = Optional.of('hello');
135-
const value = optional.orElseThrow(new Error('should not throw'));
135+
const value = optional.orElseThrow(() => new Error('should not throw'));
136136
expect(value).toBe('hello');
137137
});
138138

139139
it('should throw the given error for a None', () => {
140140
const optional = Optional.empty();
141-
expect(() => optional.orElseThrow(new Error('should throw'))).toThrowError('should throw');
141+
expect(() => optional.orElseThrow(() => new Error('should throw'))).toThrowError('should throw');
142142
});
143143
});
144144

src/optional.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export abstract class Optional<T> {
8686
* @returns The content of this Optional if it is not empty.
8787
* @throws The specified error if this Optional is empty.
8888
*/
89-
abstract orElseThrow<K extends Error>(error: K): never | NonNullable<T>;
89+
abstract orElseThrow<K extends () => Error>(func: K): never | NonNullable<T>;
9090

9191
/**
9292
compares this optional with another to determine equality.
@@ -172,7 +172,7 @@ export class None<T> extends Optional<T> {
172172
return func();
173173
}
174174

175-
orElseThrow<K extends Error>(error: K): never {
176-
throw error;
175+
orElseThrow<K extends () => Error>(func: K): never {
176+
throw func();
177177
}
178178
}

0 commit comments

Comments
 (0)