@@ -12,6 +12,7 @@ import type {
1212 NativeModuleUnionTypeAnnotation ,
1313 NativeModuleStringTypeAnnotation ,
1414 NativeModuleFunctionTypeAnnotation ,
15+ StringLiteralTypeAnnotation ,
1516 StringLiteralUnionTypeAnnotation ,
1617 UnsafeAnyTypeAnnotation ,
1718 Nullable ,
@@ -32,14 +33,44 @@ function translateUnionReturnType(
3233 type : NativeModuleEnumDeclaration | NativeModuleUnionTypeAnnotation ,
3334 options : CppCodegenOptions ,
3435) : string {
35- switch ( ( type as NativeModuleEnumDeclaration ) . memberType ) {
36- case 'StringTypeAnnotation' :
37- return options . cppStringType ;
38- case 'NumberTypeAnnotation' :
39- return 'double' ;
40- default :
41- return '::React::JSValue' ;
36+ if ( type . type === 'EnumDeclaration' ) {
37+ switch ( type . memberType ) {
38+ case 'StringTypeAnnotation' :
39+ return options . cppStringType ;
40+ case 'NumberTypeAnnotation' :
41+ return 'double' ;
42+ default :
43+ throw new Error (
44+ `Unknown enum member type in translateReturnType: ${ type . memberType } ` ,
45+ ) ;
46+ }
47+ }
48+
49+ // UnionTypeAnnotation: determine C++ type from the types array
50+ const types = type . types ;
51+ if ( types . length === 0 ) {
52+ return '::React::JSValue' ;
53+ }
54+
55+ const allString = types . every (
56+ t =>
57+ t . type === 'StringTypeAnnotation' ||
58+ t . type === 'StringLiteralTypeAnnotation' ,
59+ ) ;
60+ if ( allString ) {
61+ return options . cppStringType ;
4262 }
63+
64+ const allNumber = types . every (
65+ t =>
66+ t . type === 'NumberTypeAnnotation' ||
67+ t . type === 'NumberLiteralTypeAnnotation' ,
68+ ) ;
69+ if ( allNumber ) {
70+ return 'double' ;
71+ }
72+
73+ return '::React::JSValue' ;
4374}
4475
4576// eslint-disable-next-line complexity
@@ -49,6 +80,7 @@ export function translateFieldOrReturnType(
4980 | NativeModuleBaseTypeAnnotation
5081 | NativeModuleStringTypeAnnotation
5182 | NativeModuleFunctionTypeAnnotation
83+ | StringLiteralTypeAnnotation
5284 | StringLiteralUnionTypeAnnotation
5385 >
5486 | UnsafeAnyTypeAnnotation ,
@@ -61,7 +93,7 @@ export function translateFieldOrReturnType(
6193 const returnType = type . type ;
6294 switch ( type . type ) {
6395 case 'StringTypeAnnotation' :
64- case 'StringLiteralUnionTypeAnnotation ' :
96+ case 'StringLiteralTypeAnnotation ' :
6597 return options . cppStringType ;
6698 case 'NumberTypeAnnotation' :
6799 case 'FloatTypeAnnotation' :
@@ -88,7 +120,12 @@ export function translateFieldOrReturnType(
88120 case 'ObjectTypeAnnotation' :
89121 return getAnonymousAliasCppName ( aliases , baseAliasName , type ) ;
90122 case 'ReservedTypeAnnotation' : {
91- // ReservedTypeAnnotation.name is always 'RootTag' (#6597)
123+ // avoid: Property 'name' does not exist on type 'never'
124+ const name = type . name ;
125+ // (#6597)
126+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
127+ if ( name !== 'RootTag' )
128+ throw new Error ( `Unknown reserved function: ${ name } in ${ callerName } ` ) ;
92129 return 'double' ;
93130 }
94131 case 'TypeAliasTypeAnnotation' :
0 commit comments