Skip to content

Commit c03bddd

Browse files
authored
🤖 Merge PR DefinitelyTyped#71901 [sinon] add SinonStubbedFunction<T> type by @icholy
1 parent 4c79258 commit c03bddd

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

types/sinon/index.d.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -675,11 +675,7 @@ declare namespace Sinon {
675675
* An exception is thrown if the property is not already a function.
676676
* The original function can be restored by calling object.method.restore(); (or stub.restore();).
677677
*/
678-
<T, K extends keyof T>(
679-
obj: T,
680-
method: K,
681-
): T[K] extends (...args: infer TArgs) => infer TReturnValue ? SinonStub<TArgs, TReturnValue>
682-
: SinonStub;
678+
<T, K extends keyof T>(obj: T, method: K): SinonStubbedFunction<T[K]>;
683679
}
684680

685681
interface SinonExpectation extends SinonStub {
@@ -1482,6 +1478,13 @@ declare namespace Sinon {
14821478
type SinonStubbedMember<T> = T extends (...args: infer TArgs) => infer TReturnValue ? SinonStub<TArgs, TReturnValue>
14831479
: T;
14841480

1481+
/**
1482+
* Replaces a function type with a Sinon stub.
1483+
*/
1484+
type SinonStubbedFunction<T> = T extends (...args: infer TArgs) => infer TReturnValue
1485+
? SinonStub<TArgs, TReturnValue>
1486+
: SinonStub;
1487+
14851488
interface SinonFake {
14861489
/**
14871490
* Creates a basic fake, with no behavior

types/sinon/sinon-tests.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,16 +882,18 @@ function testTypedStub() {
882882
}
883883
let stub: sinon.SinonStub<[number, string], boolean> = sinon.stub();
884884
let stub2 = sinon.stub<[number, string], boolean>();
885+
let stub3: sinon.SinonStubbedFunction<Foo["bar"]>;
885886
const foo = new Foo();
886887
stub = sinon.stub(foo, "bar");
887888
stub2 = sinon.stub(foo, "bar");
889+
stub3 = sinon.stub(foo, "bar");
888890
const result: boolean = stub(42, "qux");
889891
const fooStub: sinon.SinonStubbedInstance<Foo> = {
890892
bar: sinon.stub(),
891893
};
892894

893-
const stub3 = sinon.stub<readonly [number, string], boolean>();
894-
stub3.firstCall.args; // $ExpectType readonly [number, string]
895+
const stub4 = sinon.stub<readonly [number, string], boolean>();
896+
stub4.firstCall.args; // $ExpectType readonly [number, string]
895897
}
896898

897899
function testMock() {

0 commit comments

Comments
 (0)