Problem: The interface of Stateless Functional Component
is given as
interface SFC<P = {}> {
(props: P & { children?: ReactNode }, context?: any): ReactElement<any> | null;
propTypes?: ValidationMap<P>;
}
The prop type of my component is also generic as:
interface Prop<V>{
num: V;
}
How to properly define my component? as:
const myCom: <T>SFC<Prop<T>> = <T>(props: Prop<T>)=> <div>test</div>
gives an error at character 27
that Cannot find name 'T'
Here is :Typescript Playground of modified example
MyFindings:
1:Typescript 2.9.1 support Stateful Generic Component: http://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-9.html#generic-type-arguments-in-jsx-elements
class myCom<T> extends React.Component<Prop<T>, any> {
render() {
return <div>test</div>;
}
}
2: Extending SFC
to make a new interface as mentioned in following answer would make component's prop type as any
:
Typescript React stateless function with generic parameter/return types which I don't want. I want to give proper type for my prop
<T>
with<T extends {}>
? – Jonas Wilmsconst myCom: <T>SFC<Prop<T>>
– Naman Kheterpal