Интерфейсный контракт, объект класса?

Хорошо - так что мне удалось заставить это работать. Текущее решение удовлетворяет двум основным требованиям для меня:

  1. Выводит defaultProps для дочернего компонента, который упаковывается, если они присутствуют, то есть компонент, сгенерированный оболочкой HoC, не нуждается в явно передать их.

  2. Предоставить объединение свойств оболочек HoC и свойств нижестоящих компонентов, чтобы Intellisense отображал все доступные свойства для упакованного компонента.

В моем стремлении упростить то, что происходит на самом деле, типы, ожидаемые функцией withHoC, жестко закодированы (в моем случае react-select), поэтому оболочка withHoC будет принимать только react-select. ] Select компонент для переноса. Все остальное, вероятно, приведет к ошибкам типа.

В этой ссылке описан некоторый код, который может автоматически выводить тип компонента, который должен быть упакован с помощью withHoC, делая withHoC многократно используемым с типами компонентов, отличными от react-select's Select ,

// node dependencies used (because dependencies mutate so much this may not work in other versions):
// "react": "^16.8.2",
// "react-dom": "^16.8.2",
// "typescript": "^3.3.3",
// "react-select": "2.4.1",
// "@types/react": "^16.8.3",
// "@types/react-dom": "^16.8.2",
// "@types/react-select": "^2.0.13",

// Visual Studio 2017 TypeScript SDK build 3.3.3

import ReactDOM from "react-dom";   // (Optional - just for testing)
import React from "react";
import Select from "react-select";

// Properties shape for React Select (See react-select @type definitions)
import { Props } from "react-select/lib/Select";

// The properties we are want to add so that our resultant wrapped component contains all of its own properties plus the extra properties specified here
interface IHOCProps {
    bar: string;
}

function withHoC(WrappedComponent: React.ComponentType) {
    return class SomeHOC extends React.Component {

        // If 'bar' isn't specified, configure a default (this section is optional)
        static defaultProps = {
            bar: "default bar"
        };

        public render(): JSX.Element {

            return <>
{this.props.bar}
; } }; } const WrappedSelect = withHoC(Select); export { WrappedSelect, Select }; // Test it out (Optional). If using Visual Studio 2017 or some other IDE with intellisense, // should show all the 'react-select' properties and the HoC property (bar). // Additionally, all the defaultProps for 'react-select' are automatically inferred so no TypeScript errors about missing props when using . const TestMe = () => <>