From 9c5cf4e9c43cf34ca27197a068075c6a393709ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ma=C5=82ecki?= Date: Tue, 25 Mar 2025 03:47:30 -0700 Subject: [PATCH] Align AnimateProps to match TS types (#50214) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/50214 This diff is a second step toward the TS AnimatedProps alignment. In this change the rest of the extended types and recursions in `WithAnimatedValue` are applied. Changelog: [Internal] - Aligned AnimateProps to match TS types. Reviewed By: huntie Differential Revision: D71623036 fbshipit-source-id: d4777e25c3bf3119608938ee4cd246cdc4c4f7ee --- .../Animated/components/AnimatedScrollView.js | 3 +- .../Animated/createAnimatedComponent.js | 45 +++++++++++++++++-- .../Animated/nodes/AnimatedValueXY.js | 4 +- .../Components/Touchable/TouchableBounce.js | 3 +- .../Components/Touchable/TouchableOpacity.js | 3 +- .../Components/View/ViewAccessibility.js | 3 +- .../__snapshots__/public-api-test.js.snap | 42 ++++++++++++++--- .../Animated/TransformStylesExample.js | 1 + .../SectionList/SectionList-scrollable.js | 1 + 9 files changed, 90 insertions(+), 15 deletions(-) diff --git a/packages/react-native/Libraries/Animated/components/AnimatedScrollView.js b/packages/react-native/Libraries/Animated/components/AnimatedScrollView.js index cd87f75be38..aafbc58bdd3 100644 --- a/packages/react-native/Libraries/Animated/components/AnimatedScrollView.js +++ b/packages/react-native/Libraries/Animated/components/AnimatedScrollView.js @@ -46,11 +46,12 @@ const AnimatedScrollView: AnimatedComponentType = props.style != null ) { return ( - // $FlowFixMe[prop-missing] + // $FlowFixMe - It should return an Animated ScrollView but it returns a ScrollView with Animated props applied. ); diff --git a/packages/react-native/Libraries/Animated/createAnimatedComponent.js b/packages/react-native/Libraries/Animated/createAnimatedComponent.js index e7314d925d5..eddbe69fcaa 100644 --- a/packages/react-native/Libraries/Animated/createAnimatedComponent.js +++ b/packages/react-native/Libraries/Animated/createAnimatedComponent.js @@ -8,7 +8,16 @@ * @format */ +import type AnimatedAddition from './nodes/AnimatedAddition'; +import type AnimatedDiffClamp from './nodes/AnimatedDiffClamp'; +import type AnimatedDivision from './nodes/AnimatedDivision'; +import type AnimatedInterpolation from './nodes/AnimatedInterpolation'; +import type AnimatedModulo from './nodes/AnimatedModulo'; +import type AnimatedMultiplication from './nodes/AnimatedMultiplication'; +import type AnimatedNode from './nodes/AnimatedNode'; import type {AnimatedPropsAllowlist} from './nodes/AnimatedProps'; +import type AnimatedSubtraction from './nodes/AnimatedSubtraction'; +import type AnimatedValue from './nodes/AnimatedValue'; import createAnimatedPropsHook from '../../src/private/animated/createAnimatedPropsHook'; import composeStyles from '../../src/private/styles/composeStyles'; @@ -18,16 +27,44 @@ import * as React from 'react'; import {useMemo} from 'react'; type Nullable = void | null; +type Primitive = string | number | boolean | symbol | void; type Builtin = (...$ReadOnlyArray) => mixed | Date | Error | RegExp; -export type WithAnimatedValue<+T> = T extends Builtin | Nullable ? T : any; +export type WithAnimatedValue<+T> = T extends Builtin | Nullable + ? T + : T extends Primitive + ? + | T + | AnimatedNode + | AnimatedAddition + | AnimatedSubtraction + | AnimatedDivision + | AnimatedMultiplication + | AnimatedModulo + | AnimatedDiffClamp + | AnimatedValue + | AnimatedInterpolation + | AnimatedInterpolation + | AnimatedInterpolation + : T extends $ReadOnlyArray + ? $ReadOnlyArray> + : T extends {...} + ? {+[K in keyof T]: WithAnimatedValue} + : T; -export type AnimatedProps = { - +[K in keyof Props]: WithAnimatedValue, -} & { +type NonAnimatedProps = 'ref' | 'innerViewRef' | 'scrollViewRef'; +type PassThroughProps = $ReadOnly<{ passthroughAnimatedPropExplicitValues?: React.ElementConfig< typeof View, > | null, +}>; + +export type AnimatedProps = { + [K in keyof (Props & PassThroughProps)]: K extends $Keys + ? PassThroughProps[K] + : K extends NonAnimatedProps + ? Props[K] + : WithAnimatedValue, }; // We could use a mapped type here to introduce acceptable Animated variants diff --git a/packages/react-native/Libraries/Animated/nodes/AnimatedValueXY.js b/packages/react-native/Libraries/Animated/nodes/AnimatedValueXY.js index f44d9e57fa6..539e54f4a16 100644 --- a/packages/react-native/Libraries/Animated/nodes/AnimatedValueXY.js +++ b/packages/react-native/Libraries/Animated/nodes/AnimatedValueXY.js @@ -214,7 +214,9 @@ export default class AnimatedValueXY extends AnimatedWithChildren { * * See https://reactnative.dev/docs/animatedvaluexy#gettranslatetransform */ - getTranslateTransform(): Array<{[key: string]: AnimatedValue, ...}> { + getTranslateTransform(): Array< + {translateX: AnimatedValue} | {translateY: AnimatedValue}, + > { return [{translateX: this.x}, {translateY: this.y}]; } diff --git a/packages/react-native/Libraries/Components/Touchable/TouchableBounce.js b/packages/react-native/Libraries/Components/Touchable/TouchableBounce.js index 25c9470e9e1..9b669f88f6b 100644 --- a/packages/react-native/Libraries/Components/Touchable/TouchableBounce.js +++ b/packages/react-native/Libraries/Components/Touchable/TouchableBounce.js @@ -173,7 +173,8 @@ class TouchableBounce extends React.Component { importantForAccessibility={ this.props['aria-hidden'] === true ? 'no-hide-descendants' - : this.props.importantForAccessibility + : // $FlowFixMe[incompatible-type] - AnimatedProps types were made more strict and need refining at this call site + this.props.importantForAccessibility } accessibilityViewIsModal={ this.props['aria-modal'] ?? this.props.accessibilityViewIsModal diff --git a/packages/react-native/Libraries/Components/Touchable/TouchableOpacity.js b/packages/react-native/Libraries/Components/Touchable/TouchableOpacity.js index eeb08c87b1a..a71e6ca982e 100644 --- a/packages/react-native/Libraries/Components/Touchable/TouchableOpacity.js +++ b/packages/react-native/Libraries/Components/Touchable/TouchableOpacity.js @@ -308,7 +308,8 @@ class TouchableOpacity extends React.Component { importantForAccessibility={ this.props['aria-hidden'] === true ? 'no-hide-descendants' - : this.props.importantForAccessibility + : // $FlowFixMe[incompatible-type] - AnimatedProps types were made more strict and need refining at this call site + this.props.importantForAccessibility } accessibilityViewIsModal={ this.props['aria-modal'] ?? this.props.accessibilityViewIsModal diff --git a/packages/react-native/Libraries/Components/View/ViewAccessibility.js b/packages/react-native/Libraries/Components/View/ViewAccessibility.js index dbfbf3a5156..e7cc4658608 100644 --- a/packages/react-native/Libraries/Components/View/ViewAccessibility.js +++ b/packages/react-native/Libraries/Components/View/ViewAccessibility.js @@ -53,7 +53,8 @@ export type AccessibilityRole = | 'webview' | 'drawerlayout' | 'slidingdrawer' - | 'iconmenu'; + | 'iconmenu' + | string; // Role types for web export type Role = diff --git a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap index 9dcad1f1850..bfe1ed4d062 100644 --- a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap +++ b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap @@ -645,14 +645,41 @@ exports[`public API should not change unintentionally Libraries/Animated/compone exports[`public API should not change unintentionally Libraries/Animated/createAnimatedComponent.js 1`] = ` "type Nullable = void | null; +type Primitive = string | number | boolean | symbol | void; type Builtin = (...$ReadOnlyArray) => mixed | Date | Error | RegExp; -export type WithAnimatedValue<+T> = T extends Builtin | Nullable ? T : any; -export type AnimatedProps = { - +[K in keyof Props]: WithAnimatedValue, -} & { +export type WithAnimatedValue<+T> = T extends Builtin | Nullable + ? T + : T extends Primitive + ? + | T + | AnimatedNode + | AnimatedAddition + | AnimatedSubtraction + | AnimatedDivision + | AnimatedMultiplication + | AnimatedModulo + | AnimatedDiffClamp + | AnimatedValue + | AnimatedInterpolation + | AnimatedInterpolation + | AnimatedInterpolation + : T extends $ReadOnlyArray + ? $ReadOnlyArray> + : T extends { ... } + ? { +[K in keyof T]: WithAnimatedValue } + : T; +type NonAnimatedProps = \\"ref\\" | \\"innerViewRef\\" | \\"scrollViewRef\\"; +type PassThroughProps = $ReadOnly<{ passthroughAnimatedPropExplicitValues?: React.ElementConfig< typeof View, > | null, +}>; +export type AnimatedProps = { + [K in keyof (Props & PassThroughProps)]: K extends $Keys + ? PassThroughProps[K] + : K extends NonAnimatedProps + ? Props[K] + : WithAnimatedValue, }; export type StrictAnimatedProps = $ReadOnly<{ ...$Exact, @@ -998,7 +1025,9 @@ declare export default class AnimatedValueXY extends AnimatedWithChildren { removeListener(id: string): void; removeAllListeners(): void; getLayout(): { [key: string]: AnimatedValue, ... }; - getTranslateTransform(): Array<{ [key: string]: AnimatedValue, ... }>; + getTranslateTransform(): Array< + { translateX: AnimatedValue } | { translateY: AnimatedValue }, + >; } " `; @@ -3800,7 +3829,8 @@ exports[`public API should not change unintentionally Libraries/Components/View/ | \\"webview\\" | \\"drawerlayout\\" | \\"slidingdrawer\\" - | \\"iconmenu\\"; + | \\"iconmenu\\" + | string; export type Role = | \\"alert\\" | \\"alertdialog\\" diff --git a/packages/rn-tester/js/examples/Animated/TransformStylesExample.js b/packages/rn-tester/js/examples/Animated/TransformStylesExample.js index 964fde91855..33bafe4c391 100644 --- a/packages/rn-tester/js/examples/Animated/TransformStylesExample.js +++ b/packages/rn-tester/js/examples/Animated/TransformStylesExample.js @@ -70,6 +70,7 @@ function AnimatedView({ Apply Selected Transforms diff --git a/packages/rn-tester/js/examples/SectionList/SectionList-scrollable.js b/packages/rn-tester/js/examples/SectionList/SectionList-scrollable.js index b4cf44c276a..41568986ecc 100644 --- a/packages/rn-tester/js/examples/SectionList/SectionList-scrollable.js +++ b/packages/rn-tester/js/examples/SectionList/SectionList-scrollable.js @@ -318,6 +318,7 @@ export function SectionList_scrollable(Props: {...}): React.MixedElement { ) } onEndReachedThreshold={0} + // $FlowFixMe[incompatible-type] - incompatible redenerItem type sections={filteredSectionData} style={styles.list} viewabilityConfig={VIEWABILITY_CONFIG}