Suport type aliases for TM getConstants() return type (#39136)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/39136

## Changelog:
[Internal] -

`getConstants()` method  for TM was enforced to only use object literals for the return type specs.

This limits flexibility, in particular those data structures can't be consequently exported and picked up by codegen (not even mentioning the potential need for copypasting those obejct literals around).

This relaxes this restriction.

Note that I've been digging into the development history in order to find out whether there was any particular historical reason for such a limitation, but couldn't find any, so I assume it was rather incidental.

Reviewed By: christophpurrer

Differential Revision: D48620652

fbshipit-source-id: 92d6ba531fc99fb9b25b4957ae123e7832f44ee4
This commit is contained in:
Ruslan Shestopalyuk
2023-08-24 06:54:03 -07:00
committed by Facebook GitHub Bot
parent 2692f206a6
commit 3c6bf7bf32
2 changed files with 23 additions and 9 deletions
@@ -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 [];
}