File tree 1 file changed +13
-5
lines changed
1 file changed +13
-5
lines changed Original file line number Diff line number Diff line change @@ -279,11 +279,19 @@ export function parseFromProgram(
279
279
throw new Error ( 'No types found' ) ;
280
280
}
281
281
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 ) ;
287
295
}
288
296
289
297
function checkType ( type : ts . Type , typeStack : number [ ] , name : string ) : t . Node {
You can’t perform that action at this time.
0 commit comments