【发布时间】:2020-05-08 06:41:39
【问题描述】:
我正在编写测试并且我正在使用模拟 history 对象,但是 TS 不断抛出以下错误:
Type '{}' is missing the following properties from type 'Location<IHistoryMock>': pathname, search, state, hash
我不确定如何解决这个问题。我尝试过扩展历史界面并以我能想到的各种方式重写它。到目前为止,这是我所拥有的:
import { History, Location } from 'history';
interface IHistoryMock extends History {
location: Location | any;
}
const historyMock: History<IHistoryMock> = { push: jest.fn(), createHref: () => '', location: { }, listen: jest.fn() };
mount(
<Router history={historyMock}>
<Component id="id" />
</Router>
);
Typescript 下划线 location: { } 并引发上述错误。我是否错误地扩展了History?我需要一个location 在这里作为一个空对象,我不想使用any 类型。
【问题讨论】:
标签: javascript reactjs typescript jestjs