【发布时间】:2022-01-05 21:10:10
【问题描述】:
有没有办法在 ts-sinon 存根构造函数上声明类型,以便打字稿理解来自 ts-sinon 和模拟类的属性和方法都可用?
下面的代码有效,但我希望foo 上的类型比any 更具体:
import { expect } from 'chai';
import * as sinon from 'ts-sinon'
class Foo {
public getFoo(): string {
return 'foo';
}
}
describe('Foo', () => {
it('can mock methods on Foo', () => {
// How can I specify a specific type that will allow properties
// and methods from both Foo and the stubConstructor return?
let foo: any = sinon.stubConstructor(Foo);
foo.getFoo.returns('bar');
expect(foo.getFoo()).to.equal('bar');
});
});
我尝试了诸如 Foo | sinon.StubbedInstance<Foo> 之类的类型,但找不到合适的类型。
【问题讨论】:
标签: typescript sinon