Skip to content

Commit

Permalink
Fix TypeScript issue with useAnimatedProps (software-mansion#4653)
Browse files Browse the repository at this point in the history
<!-- Thanks for submitting a pull request! We appreciate you spending
the time to work on these changes. Please follow the template so that
the reviewers can easily understand what the code changes affect. -->

## Summary

During the migration from manual types, the proper typing of
"useAnimatedProps" has proven to be quite challenging. As a result, I
had to resort to casting it to its previous type, but unfortunately, I
missed some places.

## Test plan

In our new docs the given
[example](https://reanimated-beta-docs.swmansion.com/docs/core/useAnimatedProps#adapters-)
doesn't work.
After applying these changes it's fixed.
  • Loading branch information
tjzel authored Jul 6, 2023
1 parent 1359a11 commit 833c864
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
13 changes: 10 additions & 3 deletions src/reanimated2/PropAdapters.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import { addWhitelistedNativeProps } from '../ConfigHelper';
import type { AdapterWorkletFunction } from './commonTypes';
import type { AnimatedPropsAdapterFunction } from './helperTypes';

export function createAnimatedPropAdapter(
// TODO TYPESCRIPT This is a temporary type to get rid of .d.ts file.
export type createAnimatedPropAdapterType = (
adapter: AnimatedPropsAdapterFunction,
nativeProps?: string[]
) => AnimatedPropsAdapterFunction;

export const createAnimatedPropAdapter = ((
adapter: AdapterWorkletFunction,
nativeProps?: string[]
): AdapterWorkletFunction {
): AdapterWorkletFunction => {
const nativePropsToAdd: { [key: string]: boolean } = {};
// eslint-disable-next-line no-unused-expressions
nativeProps?.forEach((prop) => {
nativePropsToAdd[prop] = true;
});
addWhitelistedNativeProps(nativePropsToAdd);
return adapter;
}
}) as createAnimatedPropAdapterType;

// ADAPTERS

Expand Down
14 changes: 14 additions & 0 deletions src/reanimated2/helperTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type {
SharedValue,
} from '.';
import type { ReanimatedKeyframe } from './layoutReanimation/animationBuilder/Keyframe';
import type { DependencyList } from './hook/commonTypes';

type Adaptable<T> = T | ReadonlyArray<T | ReadonlyArray<T>> | SharedValue<T>;

Expand Down Expand Up @@ -121,3 +122,16 @@ export type AnimateProps<P extends object> = NonStyleAnimatedProps<P> &
};

export type AnimatedProps<P extends object> = AnimateProps<P>;

export type AnimatedPropsAdapterFunction = (
props: Record<string, unknown>
) => void;

export type useAnimatedPropsType = <T extends object>(
updater: () => Partial<T>,
deps?: DependencyList | null,
adapters?:
| AnimatedPropsAdapterFunction
| AnimatedPropsAdapterFunction[]
| null
) => Partial<T>;
14 changes: 3 additions & 11 deletions src/reanimated2/hook/Hooks.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import { useCallback } from 'react';
import type { DependencyList } from './commonTypes';
import { useAnimatedStyle } from './useAnimatedStyle';

// TODO TYPESCRIPT This is a temporary type to get rid of .d.ts file.
type PropsAdapterFunction = (props: Record<string, unknown>) => void;

// TODO TYPESCRIPT This is a temporary type to get rid of .d.ts file.
type useAnimatedPropsType = <T extends object>(
updater: () => Partial<T>,
deps?: DependencyList | null,
adapters?: PropsAdapterFunction | PropsAdapterFunction[] | null
) => Partial<T>;
import type { DependencyList } from './commonTypes';
import type { useAnimatedPropsType } from '../helperTypes';

// TODO: we should make sure that when useAP is used we are not assigning styles
// when you need styles to animated you should always use useAS
// TODO TYPESCRIPT This is a temporary cast to get rid of .d.ts file.
export const useAnimatedProps = useAnimatedStyle as useAnimatedPropsType;

export function useWorkletCallback<A extends unknown[], R>(
Expand Down

0 comments on commit 833c864

Please sign in to comment.