mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Add support for basic unions for native modules (#46747)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46747 You can now use unions in native modules. Support for this pretty much existed, but one callsite was throwing for non-cpp modules which meant nobody could use this. Previously, StructCollector would throw that this was not supported because it couldn't generate it for objc. Instead of generating something smart in the native code for each option, it just tells native to treat them like the base type (number, string, object). Changelog: [General][Added] Codegen now supports Union Types in NativeModules Reviewed By: GijsWeterings Differential Revision: D63664505 fbshipit-source-id: 73278ed9cd64452173c5170aba44ced71181510f
This commit is contained in:
committed by
Facebook GitHub Bot
parent
af3bee6511
commit
3af126b562
Vendored
+22
-1
@@ -121,7 +121,28 @@ class StructCollector {
|
||||
case 'MixedTypeAnnotation':
|
||||
throw new Error('Mixed types are unsupported in structs');
|
||||
case 'UnionTypeAnnotation':
|
||||
throw new Error('Union types are unsupported in structs');
|
||||
switch (typeAnnotation.memberType) {
|
||||
case 'StringTypeAnnotation':
|
||||
return wrapNullable(nullable, {
|
||||
type: 'StringTypeAnnotation',
|
||||
});
|
||||
case 'NumberTypeAnnotation':
|
||||
return wrapNullable(nullable, {
|
||||
type: 'NumberTypeAnnotation',
|
||||
});
|
||||
case 'ObjectTypeAnnotation':
|
||||
// This isn't smart enough to actually know how to generate the
|
||||
// options on the native side. So we just treat it as an unknown object type
|
||||
return wrapNullable(nullable, {
|
||||
type: 'GenericObjectTypeAnnotation',
|
||||
});
|
||||
default:
|
||||
(typeAnnotation.memberType: empty);
|
||||
throw new Error(
|
||||
'Union types are unsupported in structs' +
|
||||
JSON.stringify(typeAnnotation),
|
||||
);
|
||||
}
|
||||
default: {
|
||||
return wrapNullable(nullable, typeAnnotation);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user