Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何使用typescript在窗口对象上定义browserHistory_Reactjs_Typescript_React Router - Fatal编程技术网

Reactjs 如何使用typescript在窗口对象上定义browserHistory

Reactjs 如何使用typescript在窗口对象上定义browserHistory,reactjs,typescript,react-router,Reactjs,Typescript,React Router,我是typescript新手,很难弄清楚如何在window对象上定义browserHistory。我正在使用ReactTraining提供的软件包 在我的app.jsx中,我有: import { createBrowserHistory } from 'history'; const history = createBrowserHistory({ basename: basePath, }); window.browserHistory = history; 然后在我的module

我是typescript新手,很难弄清楚如何在window对象上定义
browserHistory
。我正在使用ReactTraining提供的软件包

在我的
app.jsx
中,我有:

import { createBrowserHistory } from 'history';

const history = createBrowserHistory({
  basename: basePath,
});

window.browserHistory = history;
然后在我的
modules.d.ts
中,我有一个不起作用的代码:

import { History } from '@types/history';
interface Window { browserHistory: History; }
任何地方都是我正在使用的应用程序
窗口。browserHistory
我收到错误信息:

TS2339: Property 'browserHistory' does not exist on type 'Window'.

我可以消除这个错误,但是用
任何
替换
历史记录
,但这似乎是一种不正确的方法。npm包似乎没有明确的示例。

这是正确的语法

import { History } from 'history';

declare global {
    interface Window { browserHistory: History; }
}
可能重复的