【问题标题】:How to define browserHistory on the window object with typescript如何使用打字稿在窗口对象上定义 browserHistory
【发布时间】:2019-04-29 16:46:12
【问题描述】:

我是打字稿的新手,很难弄清楚如何在窗口对象上定义browserHistory。我正在使用来自 ReactTraining 的 history 包。

在我的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; }

任何地方都是我正在使用的应用程序window.browserHistory 我收到错误:

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

我可以让这个错误消失,但是用any 替换History 但这似乎是一种不正确的方法。 npm 包@types/history 好像没有明确的例子。

【问题讨论】:

标签: reactjs typescript react-router


【解决方案1】:

这是正确的语法

import { History } from 'history';

declare global {
    interface Window { browserHistory: History; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 2017-09-19
    • 2023-01-05
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多