How to reuse type definition in childContextTypes?

I use Typescriptfor development React, I want to use childContextTypesin Component:

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>;
    }
}

As you can see, I just want to reuse all of mine Props, but childContextTypesI need to specify everything childContextTypes, and I can not reuse the interface Props.

Can I reuse it Props? Why do I need it? At the moment I have only two details, but in some cases I may have more details, I do not want to rewrite it!

+4
source share

No one has answered this question yet.

Source: https://habr.com/ru/post/1648006/


All Articles