Sinon Stub with TypeScript

This post goes over how to use Sinon stubs with TypeScript.


Given @types/sinon is installed, stubs are automatically typed:

const object = {
  method: (name: string) => name,
const methodStub = sinon.stub(object, 'method');


Use SinonStubbedMember to type a stubbed function:

let methodStub: sinon.SinonStubbedMember<typeof object.method>;
methodStub = sinon.stub(object, 'method');


Use type assertion to specify a SinonStub:

object.method as sinon.SinonStub;

Please support this site and join our Discord!