diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js index 052a6d7b192..02ccc266898 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js @@ -355,16 +355,20 @@ function getFalsyReturnStatementFromReturnType( function buildGetConstantsMethod( method: NativeModulePropertyShape, imports: Set, + resolveAlias: AliasResolver, ): string { const [methodTypeAnnotation] = unwrapNullable(method.typeAnnotation); - if ( - methodTypeAnnotation.returnTypeAnnotation.type === 'ObjectTypeAnnotation' - ) { + let returnTypeAnnotation = methodTypeAnnotation.returnTypeAnnotation; + if (returnTypeAnnotation.type === 'TypeAliasTypeAnnotation') { + // The return type is an alias, resolve it to get the expected undelying object literal type + returnTypeAnnotation = resolveAlias(returnTypeAnnotation.name); + } + + if (returnTypeAnnotation.type === 'ObjectTypeAnnotation') { const requiredProps = []; const optionalProps = []; - const rawProperties = - methodTypeAnnotation.returnTypeAnnotation.properties || []; + const rawProperties = returnTypeAnnotation.properties || []; rawProperties.forEach(p => { if (p.optional || p.typeAnnotation.type === 'NullableTypeAnnotation') { optionalProps.push(p.name); @@ -470,7 +474,7 @@ module.exports = { const methods = properties.map(method => { if (method.name === 'getConstants') { - return buildGetConstantsMethod(method, imports); + return buildGetConstantsMethod(method, imports, resolveAlias); } const [methodTypeAnnotation] = diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js index 32a13879c94..9dcf621c709 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js @@ -459,14 +459,24 @@ function serializeConstantsProtocolMethods( ); } - const {returnTypeAnnotation} = propertyTypeAnnotation; + let {returnTypeAnnotation} = propertyTypeAnnotation; + + if (returnTypeAnnotation.type === 'TypeAliasTypeAnnotation') { + // The return type is an alias, resolve it to get the expected undelying object literal type + returnTypeAnnotation = resolveAlias(returnTypeAnnotation.name); + } + if (returnTypeAnnotation.type !== 'ObjectTypeAnnotation') { throw new Error( - `${hasteModuleName}.getConstants() may only return an object literal: {...}.`, + `${hasteModuleName}.getConstants() may only return an object literal: {...}` + + ` or a type alias of such. Got '${propertyTypeAnnotation.returnTypeAnnotation.type}'.`, ); } - if (returnTypeAnnotation.properties.length === 0) { + if ( + returnTypeAnnotation.type === 'ObjectTypeAnnotation' && + returnTypeAnnotation.properties.length === 0 + ) { return []; }