Skip to content

Commit dc2cbed

Browse files
gtomitsukafacebook-github-bot
authored andcommitted
Improve error messages for invalid function types in react-native-codegen (#36498)
Summary: The supported `CodegenTypes` aren't provided in the docs, which leads to many issues for developers working to transition native components to support Fabric. However, explicit error messages are provided in many cases (e.g., to use specific numeric types instead of `number` attributes or to remove nested optionals in arrays). This isn't the case for the common use case of TypeScript functions, which are very commonly used for event handlers in native components (such as `onChange`), forcing devs to search their way through the `react-native` codebase until they find [this file](https://github.com/facebook/react-native/blob/681d7f8113d2b5e9d6966255ee6c72b50a7d488a/Libraries/Types/CodegenTypes.js#L18). By providing an explicit error message, we can significantly improve developer experience for those working to transition libraries/components, leading to higher Fabric adoption. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [GENERAL] [ADDED] - Create explicit error message for TypeScript functions used as props in Codegen components, redirecting devs to the supported function types `BubblingEventHandler` and `DirectEventHandler`. Pull Request resolved: #36498 Test Plan: * `yarn run lint` shows no errors * no impact on `yarn && yarn jest react-native-codegen`, which failed 362 tests before and after my changes. Reviewed By: cipolleschi, sshic Differential Revision: D44132960 Pulled By: dmytrorykun fbshipit-source-id: d805ec8403613bf4e070cbd2904ff5a2648ec5fc
1 parent 4acef8e commit dc2cbed

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

packages/react-native-codegen/src/parsers/typescript/components/componentsUtils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,10 @@ function getTypeAnnotation<T>(
416416
throw new Error(
417417
`Cannot use "${type}" type annotation for "${name}": must use a specific numeric type like Int32, Double, or Float`,
418418
);
419+
case 'TSFunctionType':
420+
throw new Error(
421+
`Cannot use "${type}" type annotation for "${name}": must use a specific function type like BubblingEventHandler, or DirectEventHandler`,
422+
);
419423
default:
420424
(type: empty);
421425
throw new Error(`Unknown prop type for "${name}": "${type}"`);

0 commit comments

Comments
 (0)