Skip to content

Commit c7a87fd

Browse files
committed
fix(parser): handle optional React.ElementType
1 parent ad4dddd commit c7a87fd

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

Diff for: src/parser.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,30 @@ export function parseFromProgram(
248248

249249
function checkSymbol(symbol: ts.Symbol, typeStack: number[]): t.PropTypeNode {
250250
const declarations = symbol.getDeclarations();
251+
const declaration = declarations && declarations[0];
251252

252-
const type = declarations
253+
// TypeChecker keeps the name for { a: React.ElementType } but not
254+
// { a?: React.ElementType } can get around this by not using the typechecker
255+
if (
256+
declaration &&
257+
ts.isPropertySignature(declaration) &&
258+
declaration.type &&
259+
ts.isTypeReferenceNode(declaration.type) &&
260+
declaration.type.typeName.getText() === 'React.ElementType'
261+
) {
262+
return t.propTypeNode(
263+
symbol.getName(),
264+
getDocumentation(symbol),
265+
declaration.questionToken
266+
? t.unionNode([t.undefinedNode(), t.elementNode('elementType')])
267+
: t.elementNode('elementType'),
268+
);
269+
}
270+
271+
const type = declaration
253272
? // The proptypes aren't detailed enough that we need all the different combinations
254273
// so we just pick the first and ignore the rest
255-
checker.getTypeOfSymbolAtLocation(symbol, declarations[0])
274+
checker.getTypeOfSymbolAtLocation(symbol, declaration)
256275
: // The properties of Record<..., ...> don't have a declaration, but the symbol has a type property
257276
((symbol as any).type as ts.Type);
258277

0 commit comments

Comments
 (0)