File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -509,6 +509,16 @@ describe("transform declarations", () => {
509509 expect ( await transform ( src ) ) . toBe ( expected ) ;
510510 } ) ;
511511
512+ it ( "Converts makes sure React.Component is valid JSX, with no null for state" , async ( ) => {
513+ const src = dedent `class Foo extends React.Component<Props, null> {
514+ test(): string {return 'string'};
515+ };` ;
516+ const expected = dedent `class Foo extends React.Component<Props> {
517+ test(): string {return 'string'};
518+ };` ;
519+ expect ( await transform ( src ) ) . toBe ( expected ) ;
520+ } ) ;
521+
512522 it ( "Converts React.Node to React.ReactElement in render" , async ( ) => {
513523 const src = dedent `class Foo extends React.Component {
514524 render(): React.Node {return <div />};
Original file line number Diff line number Diff line change @@ -453,6 +453,27 @@ export function transformDeclarations({
453453 ClassDeclaration ( path ) {
454454 const { node } = path ;
455455 if ( node . superClass && node . superTypeParameters ) {
456+ // If it extends React.Component, we may need to modify the type to make sure it's still valid
457+ // in TS. Some parameters like null make it an invalid component.
458+ const isReactComponent =
459+ node . superClass . type === "MemberExpression" &&
460+ t . isIdentifier ( node . superClass . object ) &&
461+ t . isIdentifier ( node . superClass . property ) &&
462+ node . superClass . object . name === "React" &&
463+ node . superClass . property . name === "Component" ;
464+
465+ const nullSecondParam =
466+ node . superTypeParameters . params . length === 2 &&
467+ node . superTypeParameters . params [ 1 ] . type ===
468+ "NullLiteralTypeAnnotation" ;
469+
470+ // React.Component<Props, null> -> React.Component<Props> (null makes it invalid JSX)
471+ if ( isReactComponent && nullSecondParam ) {
472+ node . superTypeParameters . params =
473+ node . superTypeParameters . params . slice ( 0 , 1 ) ;
474+ }
475+
476+ // Process the type parameters
456477 node . superTypeParameters = migrateTypeParameterInstantiation (
457478 reporter ,
458479 state ,
You can’t perform that action at this time.
0 commit comments