Skip to content

Commit

Permalink
Fixed retry type definition (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlhaufe authored May 16, 2021
1 parent 43a2e19 commit 00633cc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Third party features are subject to a class's `invariant` when apply/call is used
* A contracted feature can not be applied to objects of a different base class
* Subcontract `extends` are now enforced
* Updated `retry` type declaration to support accessors.

## v0.20.5

Expand Down
7 changes: 6 additions & 1 deletion src/Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ type Properties<T extends AnyObject> = Pick<T, NonFunctionPropertyNames<T>>;
export type Invariant<T extends AnyObject> = (self: T) => boolean;
export type Demands<T extends AnyObject, F extends T[any]> = (self: T, ...args: Parameters<F>) => boolean;
export type Ensures<T extends AnyObject, F extends T[any]> = (self: T, old: Properties<T>, ...args: Parameters<F>) => boolean;
export type Rescue<T extends AnyObject, F extends T[any]> = (self: T, error: Error, args: Parameters<F>, retry: (...args: Parameters<F>) => void) => void;
export type Rescue<T extends AnyObject, F extends T[any]> = (
self: T,
error: Error,
args: F extends AnyFunc ? Parameters<F> : [F],
retry: F extends AnyFunc ? (...args: Parameters<F>) => any : (value: F) => void
) => void;

export type ContractOptions<
T extends AnyObject
Expand Down
5 changes: 2 additions & 3 deletions src/tests/rescue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('Any error thrown by a class feature must be captured by its @rescue',
value: {
rescue(self, _error, _args, retry) {
self.value = 7;
retry();
retry(7);
}
}
});
Expand Down Expand Up @@ -183,7 +183,6 @@ describe('Any error thrown by a class feature must be captured by its @rescue',
const baseContract = new Contract<Base>({
value: {
rescue(_self, _error, _args, retry) {
// @ts-ignore: typings not perfect with accessors
retry(0);
}
}
Expand Down Expand Up @@ -354,7 +353,7 @@ describe('If a `rescue` is executed and the `retry` argument is not called then
expect(base.throwRescue(true)).toBe(true);
});

test('Unrescued error', () => {
test('Un-rescued error', () => {
expect(() => base.throwFail()).toThrow('I am error');
});
});
Expand Down

0 comments on commit 00633cc

Please sign in to comment.