【发布时间】:2022-01-11 17:28:13
【问题描述】:
使用 Typescript 和 Jest,我有一个用方法定义的类
export class myAPI {
public someMethod(){...}
}
并想在其上调用 jest.spyOn,这对于像这样的直接调用效果很好
jest.spyOn(myAPI.prototype, 'someMethod')
但是当试图提供第二个参数方法作为变量时:
const theMethodName : string = 'someMethod'
jest.spyOn(myAPI.prototype, theMethodName)
我收到了Argument of type 'string' is not assignable to parameter of type 'FunctionPropertyNames<Required<myApi>>'.
如何将其类型转换为预期的类型?
【问题讨论】:
-
const string theMethodName = ...不是 TypeScript。大概你实际上有const theMethodName: string = ...- 如果你刚刚做了const theMethodName = 'someMethod',那么类型将是'someMethod',你的第一个例子显示它是有效的,而不是string。 -
你是对的 - 我更正了代码......这也为我指出了解决方案 - 谢谢!
标签: typescript jestjs ts-jest