打字稿:如何在 React 中为历史对象添加类型检查? [英] Typescript: How to add type check for history object in React?
问题描述
我有以下一段代码,它接收一个历史对象作为道具:
const ChildComponent = ({ history }) =>(<div className={styles.body}><div className={styles.cta}><FloatingActionButton onClick={() =>history.push(routes[4].path)}><span>点击我</span></FloatingActionButton>
);
我如何为这个历史道具添加类型检查,它是通过用 withRouter HOC 包装它的父级来接收的?我能想到的一种方法是写这样的东西:
接口道具{历史: {推(网址:字符串):无效;};}
但我确定这不是正确的方法,因为历史对象的其余属性正在丢失.
你能建议正确的方法吗?
根据@Oblosys 的回答更新了代码
import { withRouter, RouteComponentProps } from react-router-dom";interface Props 扩展 RouteComponentProps{/* 父组件的 props*/}class Parent 扩展 React.Component{使成为() {return ;}}//子组件相关的东西接口 ChildComponentProps 扩展了 RouteComponentProps{}const ChildComponent: React.SFC=(道具)=>(<div className={styles.body}><div className={styles.cta}><FloatingActionButton onClick={() =>history.push(routes[4].path)}><span>点击我</span></FloatingActionButton>