Skip to content

Commit a8d4b25

Browse files
committed
Add support for generic types on forwardRef
1 parent bccce70 commit a8d4b25

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

packages/react-docgen/src/utils/getTypeFromReactComponent.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
FlowType,
1313
InterfaceDeclaration,
1414
InterfaceExtends,
15+
Node,
1516
TSExpressionWithTypeArguments,
1617
TSInterfaceDeclaration,
1718
TSType,
@@ -41,6 +42,17 @@ function getStatelessPropsPath(
4142
return value.get('params')[0];
4243
}
4344

45+
function getForwardRefGenericsType(componentDefinition: NodePath) {
46+
const typeParameters = componentDefinition.get(
47+
'typeParameters',
48+
) as NodePath<Node>;
49+
if (typeParameters && typeParameters.hasNode()) {
50+
const params = typeParameters.get('params') as NodePath<Node>[];
51+
return params[1] ?? null;
52+
}
53+
return null;
54+
}
55+
4456
function findAssignedVariableType(
4557
componentDefinition: NodePath,
4658
): NodePath | null {
@@ -106,6 +118,11 @@ export default (componentDefinition: NodePath): NodePath[] => {
106118
}
107119
}
108120
} else {
121+
const typeAnnotation = getForwardRefGenericsType(componentDefinition);
122+
if (typeAnnotation) {
123+
typePaths.push(typeAnnotation);
124+
}
125+
109126
const propsParam = getStatelessPropsPath(componentDefinition);
110127

111128
if (propsParam) {

0 commit comments

Comments
 (0)