@@ -151,6 +151,10 @@ const UnqualifiedReactTypeNameMap = {
151
151
SyntheticPointerEvent : "PointerEvent" ,
152
152
SyntheticTouchEvent : "TouchEvent" ,
153
153
SyntheticTransitionEvent : "TransitionEvent" ,
154
+
155
+ // React$ElementType takes no type params, but React.ElementType takes one
156
+ // optional type param
157
+ React$ElementType : "ElementType" ,
154
158
} ;
155
159
156
160
// Only types with different names are included.
@@ -159,7 +163,7 @@ const QualifiedReactTypeNameMap = {
159
163
Text : "ReactText" ,
160
164
Child : "ReactChild" ,
161
165
Children : "ReactChildren" ,
162
- Element : "ReactElement" ,
166
+ Element : "ReactElement" , // 1:1 mapping is wrong, since ReactElement takes two type params
163
167
Fragment : "ReactFragment" ,
164
168
Portal : "ReactPortal" ,
165
169
NodeArray : "ReactNodeArray" ,
@@ -578,7 +582,94 @@ const transform = {
578
582
t . identifier ( UnqualifiedReactTypeNameMap [ typeName . name ] )
579
583
) ,
580
584
// TypeScript doesn't support empty type param lists
581
- typeParameters . params . length > 0 ? typeParameters : null
585
+ typeParameters && typeParameters . params . length > 0
586
+ ? typeParameters
587
+ : null
588
+ )
589
+ ) ;
590
+ return ;
591
+ }
592
+
593
+ if ( typeName . name === "React$Node" ) {
594
+ path . replaceWith (
595
+ t . tsTypeReference (
596
+ t . tsQualifiedName ( t . identifier ( "React" ) , t . identifier ( "ReactNode" ) )
597
+ )
598
+ ) ;
599
+ return ;
600
+ }
601
+ if ( typeName . name === "React$Element" ) {
602
+ // React$Element<T> -> React.ReactElement<React.ComponentProps<T>, T>
603
+ path . replaceWith (
604
+ t . tsTypeReference (
605
+ t . tsQualifiedName (
606
+ t . identifier ( "React" ) ,
607
+ t . identifier ( "ReactElement" )
608
+ ) ,
609
+ t . tsTypeParameterInstantiation ( [
610
+ // React.ComponentProps<T>
611
+ t . tsTypeReference (
612
+ t . tsQualifiedName (
613
+ t . identifier ( "React" ) ,
614
+ t . identifier ( "ComponentProps" )
615
+ ) ,
616
+ t . tsTypeParameterInstantiation ( [ typeParameters . params [ 0 ] ] )
617
+ ) ,
618
+ typeParameters . params [ 0 ] ,
619
+ ] )
620
+ )
621
+ ) ;
622
+ return ;
623
+ }
624
+ if ( typeName . name === "React$Component" ) {
625
+ // React$Component<Props, State> -> React.Component<Props, State>
626
+ path . replaceWith (
627
+ t . tsTypeReference (
628
+ t . tsQualifiedName ( t . identifier ( "React" ) , t . identifier ( "Component" ) ) ,
629
+ typeParameters
630
+ )
631
+ ) ;
632
+ return ;
633
+ }
634
+ if ( typeName . name === "React$ComponentType" ) {
635
+ // React$ComponentType<Props> -> React.ComponentType<Props>
636
+ path . replaceWith (
637
+ t . tsTypeReference (
638
+ t . tsQualifiedName (
639
+ t . identifier ( "React" ) ,
640
+ t . identifier ( "ComponentType" )
641
+ ) ,
642
+ typeParameters
643
+ )
644
+ ) ;
645
+ return ;
646
+ }
647
+ if ( typeName . name === "React$Context" ) {
648
+ // React$Context<T> -> React.Context<T>
649
+ path . replaceWith (
650
+ t . tsTypeReference (
651
+ t . tsQualifiedName ( t . identifier ( "React" ) , t . identifier ( "Context" ) ) ,
652
+ typeParameters
653
+ )
654
+ ) ;
655
+ return ;
656
+ }
657
+ if ( typeName . name === "React$Ref" ) {
658
+ // React$Ref<T> -> React.Ref<T>
659
+ path . replaceWith (
660
+ t . tsTypeReference (
661
+ t . tsQualifiedName ( t . identifier ( "React" ) , t . identifier ( "Ref" ) ) ,
662
+ typeParameters
663
+ )
664
+ ) ;
665
+ return ;
666
+ }
667
+ if ( typeName . name === "React$StatelessFunctionalComponent" ) {
668
+ // React$StatelessFunctionalComponent<Props> -> React.FC<Props>
669
+ path . replaceWith (
670
+ t . tsTypeReference (
671
+ t . tsQualifiedName ( t . identifier ( "React" ) , t . identifier ( "FC" ) ) ,
672
+ typeParameters
582
673
)
583
674
) ;
584
675
return ;
0 commit comments