Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,40 @@ describe('prop extraction', () => {
},
});
});

it('merges same-named prop types across union members', async () => {
const entry = await extract(
'Component',
dedent`
import React from 'react';
interface BaseProps { variant?: 'a' | 'b' }
interface VariantAProps extends BaseProps {
variant?: 'a';
valueA?: string;
valueB?: never;
}
interface VariantBProps extends BaseProps {
variant: 'b';
valueA?: never;
valueB: string;
}
type Props = VariantAProps | VariantBProps;
export const Component: React.FC<Props> = (props) => <div />;
`
);

expect(entry.component?.reactComponentMeta).toMatchObject({
props: {
variant: {
required: false,
type: {
name: 'enum',
value: [{ value: '"a"' }, { value: '"b"' }],
},
},
},
});
});
});

describe('utility types', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1255,12 +1255,14 @@ function extractPropItem(
checker: ts.TypeChecker,
prop: ts.Symbol,
contextNode: ts.Node,
defaultsMap?: Map<string, string>
defaultsMap?: Map<string, string>,
typeOverride?: ts.Type
): PropItem {
const isOptional = !!(prop.flags & typescript.SymbolFlags.Optional);
const isRequired = !isOptional;

const propType = checker.getTypeOfSymbolAtLocation(prop, contextNode);
// Union props keep one member's metadata but need the merged type.
const propType = typeOverride ?? checker.getTypeOfSymbolAtLocation(prop, contextNode);
const type = serializeType(typescript, checker, propType, isRequired);

const description = typescript.displayPartsToString(prop.getDocumentationComment(checker));
Expand Down Expand Up @@ -1530,7 +1532,20 @@ export function serializeComponentDoc(
if (excluded.has(prop.getName())) {
continue;
}
const item = extractPropItem(typescript, checker, prop, contextNode, defaultsMap);
const unionProp = isUnionType(propsType)
? checker.getPropertyOfType(propsType, prop.getName())
: undefined;
const unionPropType = unionProp
? checker.getTypeOfSymbolAtLocation(unionProp, contextNode)
: undefined;
const item = extractPropItem(
typescript,
checker,
prop,
contextNode,
defaultsMap,
unionPropType
);
if (unionForceOptional?.has(prop.getName())) {
item.required = false;
}
Expand Down