Closed
Description
TypeScript Version: 3.2.0-rc
Search Terms: react hoc 3.2.0-rc not assignable to
Code
import * as React from "react";
export interface IAnalyticsContext {
logEvent(eventName: string, details: object): void;
}
const context = React.createContext<IAnalyticsContext | null>(null);
export const AnalyticsContextProvider = context.Provider; // easy import
export const AnalyticsContextConsumer = context.Consumer; // easy import
type Omit<Value, Key extends keyof Value> = Pick<
Value,
Exclude<keyof Value, Key>
>;
export default function withAnalytics<
Props extends {
analyticsContext?: IAnalyticsContext;
},
ComponentProps = Omit<Props, "analyticsContext">
>(
Component: React.ComponentClass<Props> | React.SFC<Props>
): React.SFC<ComponentProps> {
return function AnalyticsContextBoundComponent(props: ComponentProps) {
return (
<AnalyticsContextConsumer>
{value => <Component {...props} analyticsContext={value} />} // <-- Error is on the <Component ... />
</AnalyticsContextConsumer>
);
};
}
Expected behavior:
This way of creating HOC worked without any problems in TypeScript 3.1.6, but I noticed the error in VSCode TypeScript version (3.2.0-rc), so I am wondering what is changed since this is now not a working example anymore.
Actual behavior:
[ts]
Type 'ComponentProps & { analyticsContext: IAnalyticsContext; }' is not assignable to type 'IntrinsicAttributes & Props & { children?: ReactNode; }'.
Type 'ComponentProps & { analyticsContext: IAnalyticsContext; }' is not assignable to type 'Props'.
[2322]
Possible related issues:
#27895