在我的React容器/组件中,我可以使用哪种类型来引用React路由DOM包含的match个部件?

interface Props {
  match: any // <= What could I use here instead of any?
}

export class ProductContainer extends React.Component<Props> {
  // ...
}

推荐答案

您不需要明确地添加它.你可以用@types/react-router中的RouteComponentProps<P>作为props 的基本界面.P是匹配参数的类型.

import { RouteComponentProps } from 'react-router';

// example route
<Route path="/products/:name" component={ProductContainer} />

interface MatchParams {
    name: string;
}

interface Props extends RouteComponentProps<MatchParams> {
}
// from typings
import * as H from "history";

export interface RouteComponentProps<P> {
  match: match<P>;
  location: H.Location;
  history: H.History;
  staticContext?: any;
}

export interface match<P> {
  params: P;
  isExact: boolean;
  path: string;
  url: string;
}

Typescript相关问答推荐

如何让这个函数正确推断返回类型?

自定义挂钩上的React useState正在忽略提供的初始状态

如何在类型脚本中使用条件陈述循环

使用前的组件渲染状态更新

TypScript在对象上强制执行值类型时推断对象文字类型?

React-Native运行方法通过监听另一个状态来更新一个状态

在配置对象上映射时,类型脚本不正确地推断函数参数

如何在TypeScrip面向对象程序设计中用方法/属性有条件地扩展类

如何提取密钥及其对应的属性类型,以供在新类型中使用?

在嵌套属性中使用Required

通配符路径与每条路径一起显示

为什么在回调函数的参数中不使用类型保护?

通过按键数组拾取对象的关键点

正确使用相交类型的打字集

如何在Angular 服务中制作同步方法?

如何在try 运行独立的文字脚本Angular 项目时修复Visual Studio中的MODULE_NOT_FOUND

字幕子集一个元组的多个元素

在打印脚本中将对象类型转换为数组

类型';只读<;部分<;字符串|记录<;字符串、字符串|未定义&>';上不存在TS 2339错误属性.属性&q;x&q;不存在字符串

为什么过滤器不改变可能的类型?