javascript - 如何在 TypeScript 中使用实验性装饰器和协调类型?

标签 javascript typescript

我有以下基本组件

import * as React from 'react';
import { withRouter, RouteComponentProps } from 'react-router-dom';

export interface Props { };

@withRouter
export default class Movies extends React.PureComponent<Props> {
  goBack = () => {
    this.props.history.goBack();
  };

  public render() {
    return (
      <div>
        <button onClick={this.goBack}>Back</button>
      </div>
    );
  }
}

我希望 withRouter 将特定的 props 注入(inject)我的组件。但是,当引用 this.props.history 时,我得到了这个:

enter image description here

使用将添加类型定义的装饰器的合适方法是什么?

最佳答案

我是这样做的:

import * as React from 'react';
import { withRouter, RouteComponentProps } from 'react-router-dom';

export interface OwnProps { };

type Props = OwnProps & RouteComponentProps<{}>;

@withRouter
export default class Movies extends React.PureComponent<Props> {
  goBack = () => {
    this.props.history.goBack();
  };

  public render() {
    return (
      <div>
        <button onClick={this.goBack}>Back</button>
      </div>
    );
  }
}

关于javascript - 如何在 TypeScript 中使用实验性装饰器和协调类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47888934/

相关文章:

Typescript 合并元组

angularjs - 使用 Angular 1 应用程序管理 Typescript 中的 ES6/2015 Promise

javascript - Node.js 不允许来自 api 的 CA 证书?

javascript - Google 跟踪代码管理器 - dataLayer 具有先前的值

javascript - innerHTML 不起作用,为什么?

Typescript 等同于 C++ 中的 decltype?

typescript - 如何将类型绑定(bind)添加到 graphql 解析器映射

php - 我的 JavaScript 代码在 Firefox 中运行良好,但在 chrome 和 IE 中运行不正常

javascript - Google 脚本数字格式错误

typescript - 在 tsconfig.json 中使用模式排除