给定以下代码,“子级”的类型是什么?似乎很可能只是这样,React.Component
但我不确定,因此无法仅通过仔细阅读代码就可以弄清这一点。鉴于您可以在Route中使用“组件”,因此我不确定。
此外,React.Component
由于编译器错误“错误TS2314:通用类型'Component'需要2个类型参数”,使用just失败,所以也许我可以React.Component<{}, {}>
export class App extends React.Component<{children}, {}> {
public render() {
return (
<div>
<h1>React Router Tutorial</h1>
<ul role="nav">
<li><Link to="/about">About</Link></li>
<li><Link to="/repos">Repos</Link></li>
</ul>
{this.props.children}
</div>
);
}
}
ReactDOM.render((
<Router history={hashHistory}>
<Route path="/" component={App}>
<Route path="/repos" component={Repos}/>
<Route path="/about" component={About}/>
</Route>
</Router>
), document.getElementById('app'));
为了澄清起见,我还将以另一种方式提出这个问题:说我想用以下内容替换第一行,我将给出哪种类型children
,而不是any
interface {
children: /* ??? something besides any */ any
}
export class App extends React.Component<AppProps, {}> { /* ... */ }
似乎正确的方法已在DefinitelyTyped 中的react-router.d.ts中进行了演示(具体来说,请注意截至2016年2月20日上午10:08 MST的第83行,该行定义了RouterProps,等效于Props接口众所周知的路由器组件。)
在React类型中,有一个Props接口,其中包含React组件的标准属性children
。
因此,该代码示例将转换为以下内容。特别注意顶部的界面。
interface AppProps extends React.Props<App> { }
export class App extends React.Component<AppProps, {}> {
/* ... */
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句