From 3eb330a36558be3345f60699a1e2bf4852e1a410 Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Tue, 18 Aug 2026 06:45:17 -0700 Subject: [PATCH] Type Animated.event() as an event handler instead of any (#57991) Summary: Pull Request resolved: https://github.com/react/react-native/pull/57991 Addresses a type narrowness regression (Strict API vs legacy manual types), raised by user feedback: https://github.com/react-native-community/discussions-and-proposals/discussions/1015#discussioncomment-18053943 Changelog: [General][Fixed] - **Animated**: `Animated.event()` is typed as an event handler rather than `any` Reviewed By: vzaidman Differential Revision: D116440620 fbshipit-source-id: 4ba629c511fcf28981700d1bc841a4ce8dcf17f8 --- packages/react-native/Libraries/Animated/AnimatedEvent.js | 2 +- .../Libraries/Animated/AnimatedImplementation.js | 8 ++++++-- packages/react-native/ReactNativeApi.d.ts | 4 ++-- .../src/private/animated/__tests__/AnimatedNative-test.js | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/react-native/Libraries/Animated/AnimatedEvent.js b/packages/react-native/Libraries/Animated/AnimatedEvent.js index 866e9bdb20d..4704a87e421 100644 --- a/packages/react-native/Libraries/Animated/AnimatedEvent.js +++ b/packages/react-native/Libraries/Animated/AnimatedEvent.js @@ -197,7 +197,7 @@ export class AnimatedEvent { this._attachedEvent && this._attachedEvent.detach(); } - __getHandler(): any | ((...args: any) => void) { + __getHandler(): (...args: any) => void { if (this.__isNative) { if (__DEV__) { let validatedMapping = false; diff --git a/packages/react-native/Libraries/Animated/AnimatedImplementation.js b/packages/react-native/Libraries/Animated/AnimatedImplementation.js index 4a9d76a4806..7fc01bb2724 100644 --- a/packages/react-native/Libraries/Animated/AnimatedImplementation.js +++ b/packages/react-native/Libraries/Animated/AnimatedImplementation.js @@ -576,10 +576,14 @@ function unforkEventImpl( } } -const eventImpl = function ( +// NOTE: With `useNativeDriver: true` this returns an `AnimatedEvent` instance +// rather than a callable handler. That object is only ever meant to be handed +// straight back to an animated component's event prop, so the declared type +// describes the handler shape both branches are consumed as. +const eventImpl: ( argMapping: ReadonlyArray, config: EventConfig, -): any { +) => (...args: Array) => void = function (argMapping, config): any { const animatedEvent = new AnimatedEvent(argMapping, config); if (animatedEvent.__isNative) { return animatedEvent; diff --git a/packages/react-native/ReactNativeApi.d.ts b/packages/react-native/ReactNativeApi.d.ts index a04517cbd5a..0b667e3cc6e 100644 --- a/packages/react-native/ReactNativeApi.d.ts +++ b/packages/react-native/ReactNativeApi.d.ts @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<<44990574a74bcc122718d4f6142db8cd>> + * @generated SignedSource<<5335b26453a7d4d5bb9b7d54ded6218f>> * * This file was generated by scripts/js-api/build-types/index.js. */ @@ -232,7 +232,7 @@ declare const event: typeof $$AnimatedImplementation.event declare const eventImpl: ( argMapping: ReadonlyArray, config: EventConfig, -) => any +) => (...args: Array) => void declare const findNodeHandle: typeof $$ReactFabric.findNodeHandle declare const flatten: typeof $$flattenStyle declare const forkEvent: typeof $$AnimatedImplementation.forkEvent diff --git a/packages/react-native/src/private/animated/__tests__/AnimatedNative-test.js b/packages/react-native/src/private/animated/__tests__/AnimatedNative-test.js index 667afdd9fb7..0141d70dd6c 100644 --- a/packages/react-native/src/private/animated/__tests__/AnimatedNative-test.js +++ b/packages/react-native/src/private/animated/__tests__/AnimatedNative-test.js @@ -369,7 +369,7 @@ describe('Native Animated', () => { const value = new Animated.Value(0); value.__makeNative(); const listener = jest.fn(); - const event = Animated.event([{nativeEvent: {foo: value}}], { + const event = new Animated.Event([{nativeEvent: {foo: value}}], { useNativeDriver: true, listener, });