Typescript和Jest:mocking抛出TypeError,因为它使用了错误的重载

Typescript和Jest:mocking抛出TypeError,因为它使用了错误的重载,typescript,unit-testing,jestjs,Typescript,Unit Testing,Jestjs,在一个玩笑测试中给出以下内容 const mockDirListing:string[]=['sdafd','sfdf']; const mockReaddirSync=jest.spyOn(fs,'readdirSync'); mockReaddirSync.mockReturnValue(mockDirListing); TypeScript将出现以下错误: Argument of type 'string[]' is not assignable to parameter of type

在一个玩笑测试中给出以下内容

const mockDirListing:string[]=['sdafd','sfdf'];
const mockReaddirSync=jest.spyOn(fs,'readdirSync');
mockReaddirSync.mockReturnValue(mockDirListing);
TypeScript将出现以下错误:

Argument of type 'string[]' is not assignable to parameter of type 'Dirent[]'.
Type 'string' is not assignable to type 'Dirent'.ts(2345)
模拟实例的类型为

jest.SpyInstance<fs.Dirent[], [fs.PathLike, {
    encoding?: string;
    withFileTypes: true;
}]>
投掷

Argument of type '(a: string) => string' is not assignable to parameter of type '(a: number) => string'.
  Types of parameters 'a' and 'a' are incompatible.
    Type 'number' is not assignable to type 'string'.ts(2345)

如何使用第一个重载的类型签名?

我们将在研究它之后,看起来您能做的最好的事情是

const stub = (sinon.stub(fs, 'readdir') as unknown) as sinon.SinonStub<any[], Promise<Dirent[]>>;
const stub=(sinon.stub(fs,'readdir')为未知)为sinon.SinonStub;
一旦指定,将类型强制为

Argument of type '(a: string) => string' is not assignable to parameter of type '(a: number) => string'.
  Types of parameters 'a' and 'a' are incompatible.
    Type 'number' is not assignable to type 'string'.ts(2345)
const stub = (sinon.stub(fs, 'readdir') as unknown) as sinon.SinonStub<any[], Promise<Dirent[]>>;