mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Modify native module codegen to throw JS errors when passing null values for non-nullable arguments
Summary: Changelog: [General][Breaking] Native modules using the codegen now throw an error when called with `null` for optional but not nullable arguments. ## Context Right now, if you have a native module using the codegen with a method like this: ``` someMethod(value?: number): void; ``` And you call it like this: ``` NativeModule.someMethod(null); ``` The app doesn't throw an error, but it should because this method shouldn't accept `null` according to its type definition. ## Changes This modifies the codegen to only check for `undefined` in those cases, otherwise trying to cast the value to the expected type and failing if it's `null`. NOTE: this is technically a breaking change, but if people are using Flow or TypeScript in their projects they're very unlikely to hit this case, because they would've complained if you tried to pass `null` in these cases. Reviewed By: cipolleschi Differential Revision: D54206289 fbshipit-source-id: 58f2f2f3009d203b96189d3c66d1ae98a9e4fb36
This commit is contained in:
committed by
Facebook GitHub Bot
parent
21171222eb
commit
67b9628af5
@@ -132,6 +132,12 @@ function serializeArg(
|
||||
const val = `args[${index}]`;
|
||||
const expression = callback(val);
|
||||
|
||||
// param?: T
|
||||
if (optional && !nullable) {
|
||||
// throw new Error('are we hitting this case? ' + moduleName);
|
||||
return `count <= ${index} || ${val}.isUndefined() ? std::nullopt : std::make_optional(${expression})`;
|
||||
}
|
||||
|
||||
// param: ?T
|
||||
// param?: ?T
|
||||
if (nullable || optional) {
|
||||
|
||||
Reference in New Issue
Block a user