Skip to content

Commit af117aa

Browse files
✨ forwardRef wrapped in a memo now works with type inference (#163)
1 parent 64571f8 commit af117aa

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

packages/extract-react-types/__snapshots__/converters-typescript.test.js.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,34 @@ Object {
452452
}
453453
`;
454454

455+
exports[`TypeScript: memo wrapping forwardRef, with inferred types 1`] = `
456+
Object {
457+
"kind": "generic",
458+
"name": Object {
459+
"kind": "id",
460+
"name": "MyComponent",
461+
"type": null,
462+
},
463+
"value": Object {
464+
"kind": "object",
465+
"members": Array [
466+
Object {
467+
"key": Object {
468+
"kind": "id",
469+
"name": "foo",
470+
},
471+
"kind": "property",
472+
"optional": false,
473+
"value": Object {
474+
"kind": "string",
475+
},
476+
},
477+
],
478+
"referenceIdName": "MyComponentProps",
479+
},
480+
}
481+
`;
482+
455483
exports[`TypeScript: ts any 1`] = `
456484
Object {
457485
"kind": "object",

packages/extract-react-types/converters-typescript.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,23 @@ const TESTS = [
601601
602602
export default MyComponent;
603603
`
604+
},
605+
{
606+
name: 'memo wrapping forwardRef, with inferred types',
607+
typeSystem: 'typescript',
608+
code: `
609+
import React, { forwardRef, memo } from 'react';
610+
611+
type MyComponentProps = {
612+
foo: string,
613+
}
614+
615+
const MyComponent = memo(forwardRef<HTMLElement, MyComponentProps>((props, ref) => {
616+
return <span>Foo</span>;
617+
}));
618+
619+
export default MyComponent;
620+
`
604621
}
605622
];
606623

packages/extract-react-types/src/index.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,6 @@ function exportedComponents(programPath, componentsToFind: 'all' | 'default', co
13891389
);
13901390

13911391
components.push({ name, path, component });
1392-
13931392
return;
13941393
}
13951394

@@ -1401,7 +1400,6 @@ function exportedComponents(programPath, componentsToFind: 'all' | 'default', co
14011400
);
14021401

14031402
components.push({ name, path, component });
1404-
14051403
return;
14061404
}
14071405

@@ -1419,6 +1417,23 @@ function exportedComponents(programPath, componentsToFind: 'all' | 'default', co
14191417
return;
14201418
}
14211419

1420+
const genericTypeParamsTEMP = firstArg.get('typeParameters');
1421+
1422+
if (
1423+
isMemo &&
1424+
isSpecialReactComponentType(firstArg, 'forwardRef') &&
1425+
genericTypeParamsTEMP.node
1426+
) {
1427+
const component = convertReactComponentFunction(
1428+
genericTypeParams,
1429+
context,
1430+
genericTypeParamsTEMP.get('params.1')
1431+
);
1432+
1433+
components.push({ name, path, component });
1434+
return;
1435+
}
1436+
14221437
if (isMemo && isSpecialReactComponentType(firstArg, 'forwardRef')) {
14231438
const innerFirstArg = firstArg.get('arguments')[0];
14241439
if (innerFirstArg.isFunctionExpression() || innerFirstArg.isArrowFunctionExpression()) {
@@ -1427,11 +1442,7 @@ function exportedComponents(programPath, componentsToFind: 'all' | 'default', co
14271442
innerFirstArg,
14281443
innerFirstArg.get('params.0.typeAnnotation')
14291444
);
1430-
components.push({
1431-
name,
1432-
path,
1433-
component
1434-
});
1445+
components.push({ name, path, component });
14351446
}
14361447
}
14371448
}

0 commit comments

Comments
 (0)