如何在childContextTypes中重用类型定义?

rog*_*ger 5 typescript reactjs

我使用的Typescript开发React,我想用childContextTypesComponent:

import * as React from "react";

interface Props {
  foo: string;
  bar: string;
}

export class Parent extends React.Component<Props, {}> {
    constructor(props:Props, context:any) {
      super(props, context);
    }

    static childContextTypes =  {
      foo: React.PropTypes.string.isRequired,
        bar: React.PropTypes.string.isRequired,
    }

    getChildContext() {
        return this.props;
    }

    render() {
        return <Child />
    }
}

class Child extends React.Component<{}, {}> {
    context: any;

    static contextTypes = {
      foo: React.PropTypes.string.isRequired,
        bar: React.PropTypes.string.isRequired
    }

    render() {
      return <div>Hello, {this.context.foo}, {this.context.bar}</div>;
    }
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我只想重用我的所有Props,但在childContextTypes我必须指定所有childContextTypes并且我不能重用Props接口.

我能重复使用Props吗?为什么我需要这个?现在,我只有两个道具,但在某些条件下我可能有更多的道具,我不想重写它!