Skip to content

Commit 30f56ec

Browse files
committed
fix(parser): handle optional any
1 parent c7a87fd commit 30f56ec

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/parser.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,19 @@ export function parseFromProgram(
279279
throw new Error('No types found');
280280
}
281281

282-
return t.propTypeNode(
283-
symbol.getName(),
284-
getDocumentation(symbol),
285-
checkType(type, typeStack, symbol.getName()),
286-
);
282+
// Typechecker only gives the type "any" if it's present in a union
283+
// This means the type of "a" in {a?:any} isn't "any | undefined"
284+
// So instead we check for the questionmark to detect optional types
285+
let parsedType: t.Node | undefined = undefined;
286+
if (type.flags & ts.TypeFlags.Any && declaration && ts.isPropertySignature(declaration)) {
287+
parsedType = declaration.questionToken
288+
? t.unionNode([t.undefinedNode(), t.anyNode()])
289+
: t.anyNode();
290+
} else {
291+
parsedType = checkType(type, typeStack, symbol.getName());
292+
}
293+
294+
return t.propTypeNode(symbol.getName(), getDocumentation(symbol), parsedType);
287295
}
288296

289297
function checkType(type: ts.Type, typeStack: number[], name: string): t.Node {

0 commit comments

Comments
 (0)