使用类型化参数进行路由

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Route, BrowserRouter as Router, Link, match } from 'react-router-dom';

// define React components for multiple pages
class Home extends React.Component<any, any> {
  render() {
    return (
      <div>
        <div>HOME</div>
        <div><Link to='/details/id123'>Goto Details</Link></div>
      </div>);
  }
}

interface DetailParams {
  id: string;
}

interface DetailsProps {
  required: string;
  match?: match<DetailParams>;
}

class Details extends React.Component<DetailsProps, any> {
  render() {
    const match = this.props.match;
    if (match) {
      return (
        <div>
          <div>Details for {match.params.id}</div>
          <Link to='/'>Goto Home</Link>
        </div>
      );
    } else {
      return (
        <div>
          <div>Error Will Robinson</div>
          <Link to='/'>Goto Home</Link>
        </div>
      )
    }
  }
}

ReactDOM.render(
  <Router>
    <div>
      <Route exact path="/" component={Home} />
      <Route exact path="/details/:id" component={(props) => <Details required="some string" {...props} />} />
    </div>
  </Router>

  , document.getElementById('root')
);

为了保护具有所需属性 requiredDetails 组件的类型安全性,<Route> 定义定义了一个基于匿名函数的组件,它组成了 <Details> 类型的新组件并指定了 required 属性。

扩展运算符用于将传递给基于匿名函数的组件的 props 重新应用到组合的 <Details> 组件上。

match 属性被定义为可选,因为它由 react-router 动态填充,遗憾的是,我们无法将其定义为必需属性。这意味着稍后访问值时需要类型保护。