You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeComponentClass<P>=new(props: P)=>Component<P>;declareclassComponent<P>{props: P;constructor(props: P);}declarefunctionmyHoc<P>(C: ComponentClass<P>): ComponentClass<P>;typeNestedProps<T>={foo: number,stuff: T};declareclassGenericComponent<T>extendsComponent<NestedProps<T>>{}// type is 'new <T>(props: NestedProps<T>) => Component<NestedProps<T>>'constGenericComponent2=myHoc(GenericComponent);
Expected behavior:
Should work the same when using "React.Component/Class" instead of inline definitions.
declare function myHoc<P>(C: React.ComponentClass<P>): React.ComponentClass<P>;
type NestedProps<T> = { foo: number, stuff: T };
declare class GenericComponent<T> extends React.Component<NestedProps<T>> {
}
// type should be 'new <T>(props: NestedProps<T>) => React.Component<NestedProps<T>>'
// actual type is 'React.ComponentClass<NestedProps<unknown>, any>'
const GenericComponent2 = myHoc(GenericComponent);
Actual behavior:
The component returned from the HOC loses it's generic type parameter and instead uses unknown.
React.ComponentClass<NestedProps<unknown>, any>
I was able to narrow down the issue to the ComponentClass definition. Using the example type works, or using an simple interface works. But if the ComponentClass definition has any other properties this behavior appears.
// works
type ComponentClass<P> = new (props: P) => Component<P>;
// works
interface ComponentClass<P = {}> {
new (props: P, context?: any): Component<P>;
}
// does not work
interface ComponentClass<P = {}> {
new (props: P, context?: any): Component<P>;
displayName?: string;
}
TypeScript Version: typescript@3.5.0-dev.20190518
Search Terms:
hoc, Higher order type inference
Code
Here is the code from the example: https://devblogs.microsoft.com/typescript/announcing-typescript-3-5-rc/#higher-order-type-inference-from-generic-constructors
Expected behavior:
Should work the same when using "React.Component/Class" instead of inline definitions.
Actual behavior:
The component returned from the HOC loses it's generic type parameter and instead uses unknown.
I was able to narrow down the issue to the ComponentClass definition. Using the example type works, or using an simple interface works. But if the ComponentClass definition has any other properties this behavior appears.
Playground Link:
Related Issues:
#30650
The text was updated successfully, but these errors were encountered: