@@ -222,16 +222,23 @@ export function reactJSMakePropsAndStateInterfaceTransformFactoryFactory(typeChe
222
222
console . warn ( 'Bad value for propType' , name , 'at' , propertyAssignment . getStart ( ) ) ;
223
223
return result ;
224
224
}
225
- const typeValue = getTypeFromReactPropTypeExpression ( propertyAssignment . initializer ) ;
226
225
227
226
// Ignore children, React types have it
228
227
if ( propertyAssignment . name . getText ( ) === 'children' ) {
229
228
return result ;
230
229
}
230
+
231
+ // Ignore children, React types have it
232
+ if ( propertyAssignment . name . getText ( ) === 'children' ) {
233
+ return result ;
234
+ }
235
+
236
+ const typeValue = getTypeFromReactPropTypeExpression ( propertyAssignment . initializer ) ;
237
+ const isOptional = isPropTypeOptional ( propertyAssignment . initializer ) ;
231
238
const propertySignature = ts . createPropertySignature (
232
239
[ ] ,
233
240
name ,
234
- undefined ,
241
+ isOptional ? ts . createToken ( ts . SyntaxKind . QuestionToken ) : undefined ,
235
242
typeValue ,
236
243
undefined ,
237
244
) ;
@@ -291,19 +298,16 @@ export function reactJSMakePropsAndStateInterfaceTransformFactoryFactory(typeChe
291
298
} else {
292
299
result = ts . createKeywordTypeNode ( ts . SyntaxKind . AnyKeyword ) ;
293
300
}
301
+ return result ;
302
+ }
294
303
295
- if ( ! / \. i s R e q u i r e d / . test ( text ) ) {
296
- return makeTypeNodeOptional ( result ) ;
297
- } else {
298
- return result ;
299
- }
300
-
301
- function makeTypeNodeOptional ( node : ts . TypeNode ) {
302
- return ts . createUnionOrIntersectionTypeNode ( ts . SyntaxKind . UnionType , [
303
- node ,
304
- ts . createKeywordTypeNode ( ts . SyntaxKind . UndefinedKeyword )
305
- ] ) ;
306
- }
304
+ /**
305
+ * Decide if node is optional
306
+ * @param node React propTypes member node
307
+ */
308
+ function isPropTypeOptional ( node : ts . PropertyAccessExpression ) {
309
+ const text = node . getText ( ) . replace ( / R e a c t \. P r o p T y p e s \. / , '' ) ;
310
+ return ! / \. i s R e q u i r e d / . test ( text )
307
311
}
308
312
} ;
309
313
} ;
0 commit comments