diff --git a/packages/react-native-codegen/src/parsers/parsers-commons.js b/packages/react-native-codegen/src/parsers/parsers-commons.js index 4c35842bb98..20f5ec39685 100644 --- a/packages/react-native-codegen/src/parsers/parsers-commons.js +++ b/packages/react-native-codegen/src/parsers/parsers-commons.js @@ -189,30 +189,42 @@ function getObjectTypeAnnotations( if (!isTypeAlias) { return; } - const parent = parser.nextNodeForTypeAlias(value); + let parent = parser.nextNodeForTypeAlias(value); + // Unwrap $ReadOnly/Readonly wrappers to get the inner object type + if ( + parent.type === 'GenericTypeAnnotation' && + (parent.id?.name === '$ReadOnly' || parent.id?.name === 'Readonly') && + parent.typeParameters?.params?.length === 1 + ) { + parent = parent.typeParameters.params[0]; + } else if ( + parent.type === 'TSTypeReference' && + parent.typeName?.name === 'Readonly' && + parent.typeParameters?.params?.length === 1 + ) { + parent = parent.typeParameters.params[0]; + } if ( parent.type !== 'ObjectTypeAnnotation' && parent.type !== 'TSTypeLiteral' ) { return; } - const typeProperties = parser - .getAnnotatedElementProperties(value) - .map(prop => - parseObjectProperty( - parent, - prop, - hasteModuleName, - types, - aliasMap, - {}, // enumMap - tryParse, - true, // cxxOnly - prop?.optional || false, - translateTypeAnnotation, - parser, - ), - ); + const typeProperties = (parent.properties ?? parent.members).map(prop => + parseObjectProperty( + parent, + prop, + hasteModuleName, + types, + aliasMap, + {}, // enumMap + tryParse, + true, // cxxOnly + prop?.optional || false, + translateTypeAnnotation, + parser, + ), + ); aliasMap[key] = { type: 'ObjectTypeAnnotation', properties: typeProperties, diff --git a/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.js b/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.js index 029c11cbcf0..ad01c754904 100644 --- a/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.js +++ b/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.js @@ -31,11 +31,11 @@ export type UnionFloat = 1.44 | 2.88 | 5.76; export type UnionString = 'One' | 'Two' | 'Three'; export type UnionObject = {value: number} | {low: string}; -export type ConstantsStruct = { +export type ConstantsStruct = Readonly<{ const1: boolean, const2: number, const3: string, -}; +}>; export type ObjectStruct = { a: number,