There is a code below. Code is trying to make a class to create React-component handlers.

import * as React from 'react' import { Component } from 'react' type PropTypes = { str: string } // Класс компонента class MyComponent extends Component<PropTypes> { handlers: Handlers constructor(props) { super(props) this.handlers = new Handlers(this) } public render() { return <div onClick={this.handlers.onClick}/> } } // Абстрактный класс для создания классов с обработчиками abstract class HandlersCreator<C extends Component> { props: Pick<C, 'props'> // Пытаемся присвоить тип «props». // Как-то нужно здесь использовать PropTypes, полученный из «C» constructor(public component: Readonly<C>) { // Тут происходит ругань, что нельзя присвоить, ибо типы // 'C["props"]' и 'Readonly<{ children?: ReactNode }> & Readonly<{}>' // несовместимы this.props = component.props } } // Класс с обработчиками class Handlers extends HandlersCreator<MyComponent> { constructor(component: MyComponent) { super(component) // Тут тоже ошибка // Property 'str' does not exist on type 'Pick<MyComponent, "props">' console.log(this.props.str) } public onClick() { // Тут колдунство для клика } } 

It is required in the HandlerCreator to get the property type “props” of the reactant component. I suppose you need to get it from generic C. Somehow. You can't use PropTypes directly. How to be?

    1 answer 1

    Everything turned out to be much simpler than I thought:

     interface HandlersCreator<C extends Component> { props: C['props'] }