Skip to content

Commit d4e6aa4

Browse files
committed
fix: support Babel 8 mapped type fields
1 parent c98b177 commit d4e6aa4

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

packages/react-docgen/src/__tests__/babel8-test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,23 @@ test('parses generic arrow functions in TypeScript files with Babel 8', () => {
5353

5454
expect(result).toHaveLength(1);
5555
});
56+
57+
test('parses mapped TypeScript props with Babel 8', () => {
58+
const result = parse(
59+
`export type StatusFiltersProps<K extends string = string> = {
60+
statuses?: { readonly [Key in K]: number };
61+
};
62+
63+
export function StatusFilters<K extends string = string>(
64+
_props: StatusFiltersProps<K>,
65+
) {
66+
return <div />;
67+
}`,
68+
{ filename: 'index.tsx' },
69+
);
70+
71+
expect(result[0]?.props?.statuses?.tsType).toMatchObject({
72+
name: 'signature',
73+
type: 'object',
74+
});
75+
});

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,12 @@ function handleTSMappedType(
285285
path: NodePath<TSMappedType>,
286286
typeParams: TypeParameters | null,
287287
): ObjectSignatureType<TSFunctionSignatureType> {
288-
const key = getTSTypeWithResolvedTypes(
289-
path.get('typeParameter').get('constraint') as NodePath<TSType>,
290-
typeParams,
291-
);
288+
const constraint = (
289+
'constraint' in path.node
290+
? path.get('constraint')
291+
: path.get('typeParameter').get('constraint')
292+
) as NodePath<TSType>;
293+
const key = getTSTypeWithResolvedTypes(constraint, typeParams);
292294

293295
key.required = !path.node.optional;
294296

0 commit comments

Comments
 (0)