Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#60089 Opossum: Add type for `CircuitBreake…
Browse files Browse the repository at this point in the history
…r#call` by @steevsachs

* types: Add type for `CircuitBreaker#call`

* Respond to @tjenkinson review
* Spread args in test proxyFn to make clear it's a rest argument

* Respond to @tjenkinson review
* Add explicit `number[]` type annotation to `args` for future-proofing
  • Loading branch information
steevsachs authored Apr 27, 2022
1 parent 34e20b8 commit 658793d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions types/opossum/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ declare class CircuitBreaker<TI extends unknown[] = unknown[], TR = unknown> ext
readonly warmUp: boolean;
readonly volumeThreshold: number;

/**
* Execute the action for this circuit with a provided this argument
*/
call(context: any, ...args: TI): Promise<TR>;

/**
* Clears the cache of this CircuitBreaker
*/
Expand Down
15 changes: 15 additions & 0 deletions types/opossum/opossum-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,18 @@ window[0].fires; // $ExpectType number
const noTimeoutOptions: CircuitBreaker.Options = {
timeout: false, // false value deactivate timeout
};

// you can call with a provided this arg
const context = {test: 'test' as const};
async function proxyFn(this: typeof context, ...args: number[]) {
return {
args,
thisArg: this.test,
};
}
const args: number[] = [1, 2, 3];
const callBreaker = new CircuitBreaker(proxyFn, options);
callBreaker.call(context, ...args).then((result) => {
result.args; // $ExpectType number[]
result.thisArg; // $ExpectType "test"
});

0 comments on commit 658793d

Please sign in to comment.