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

时间:2017-12-19 14:17:24

标签: 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能够在我的组件中注入特定的道具。但是,在引用this.props.history时,我得到了这个:

enter image description here

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

1 个答案:

答案 0 :(得分:1)

我是这样做的:

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>
    );
  }
}