react-native-code-gen Add Union Type support for C++ TurboModules

Summary:
There are cases where we want to pass a union type into a TurboModule method which is handy to restrict certain arguments to a restricted set of values, e.g. restricting quality to ```'low'```, ```high``` or resolution to ```720```, ```1080```.

- We are not generating an ```union``` type in C++ but rather just cast type safe to the corresponding member type.

- We don't support mixed primitive union types at this time, e.g. ```export type ChooseSomething = 'One' | 'Two' | 3 | false;``` does not work.

- We can support mixed object union types such as ... ```export type ChooseObject = {} | {low: string};``` - which need special logic in the C++ TM to correctly parse the resulting jsi::Object

This is for C++ only, Java and ObjC are not supported atm.

Changelog:
[General][Added] - react-native-code-gen Add Union Type support for C++ TurboModules

Reviewed By: javache

Differential Revision: D38919688

fbshipit-source-id: 0fd37545b32b4f2059a8babda62dab4a85de37a9
This commit is contained in:
Christoph Purrer
2022-08-24 14:47:39 -07:00
committed by Facebook GitHub Bot
parent f3def13a92
commit 355feafff6
18 changed files with 348 additions and 45 deletions
@@ -156,7 +156,7 @@ function translateFunctionParamToJavaType(
imports.add('com.facebook.react.bridge.Callback');
return 'Callback';
default:
(realTypeAnnotation.type: 'MixedTypeAnnotation');
(realTypeAnnotation.type: 'MixedTypeAnnotation' | 'UnionTypeAnnotation');
throw new Error(createErrorMessage(realTypeAnnotation.type));
}
}
@@ -220,7 +220,7 @@ function translateFunctionReturnTypeToJavaType(
imports.add('com.facebook.react.bridge.WritableArray');
return wrapIntoNullableIfNeeded('WritableArray');
default:
(realTypeAnnotation.type: 'MixedTypeAnnotation');
(realTypeAnnotation.type: 'MixedTypeAnnotation' | 'UnionTypeAnnotation');
throw new Error(createErrorMessage(realTypeAnnotation.type));
}
}
@@ -272,7 +272,7 @@ function getFalsyReturnStatementFromReturnType(
case 'ArrayTypeAnnotation':
return 'return null;';
default:
(realTypeAnnotation.type: 'MixedTypeAnnotation');
(realTypeAnnotation.type: 'MixedTypeAnnotation' | 'UnionTypeAnnotation');
throw new Error(createErrorMessage(realTypeAnnotation.type));
}
}