Skip to content

Commit

Permalink
Add Animated.Numeric Flow type
Browse files Browse the repository at this point in the history
Summary:
Changelog: [General][Added] - Add `Animated.Numeric` Flow type

Adds a Flow type to represent the various Animated node types that evaluate to numeric values and can be `interpolate()`d.

I'm including `AnimatedInterpolation` as "numeric" here even though it can technically evaluate either to a number or to a string, depending on its config. Note that calling `interpolate()` on a string-valued `AnimatedInterpolation` is a runtime error.

In a future diff, I'm planning to add a type argument to `AnimatedInterpolation` (and its config type), at which point we can refine `Animated.Numeric` to correctly include only `AnimatedInterpolation<number>`.

Reviewed By: javache

Differential Revision: D35869375

fbshipit-source-id: 2ff6754f1a5abc68c9da2c6836872c2022b25676
  • Loading branch information
motiz88 authored and facebook-github-bot committed May 3, 2022
1 parent 79529a1 commit 9eb7629
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Libraries/Animated/Animated.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import typeof AnimatedSectionList from './components/AnimatedSectionList';
import typeof AnimatedText from './components/AnimatedText';
import typeof AnimatedView from './components/AnimatedView';

const AnimatedMock = require('./AnimatedMock');
const AnimatedImplementation = require('./AnimatedImplementation');
import * as AnimatedMock from './AnimatedMock';
import * as AnimatedImplementation from './AnimatedImplementation';

const Animated = ((Platform.isTesting
? AnimatedMock
Expand Down
13 changes: 13 additions & 0 deletions Libraries/Animated/AnimatedImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,19 @@ const event = function (
}
};

// All types of animated nodes that represent scalar numbers and can be interpolated (etc)
type AnimatedNumeric =
| AnimatedAddition
| AnimatedDiffClamp
| AnimatedDivision
| AnimatedInterpolation
| AnimatedModulo
| AnimatedMultiplication
| AnimatedSubtraction
| AnimatedValue;

export type {AnimatedNumeric as Numeric};

/**
* The `Animated` library is designed to make animations fluid, powerful, and
* easy to build and maintain. `Animated` focuses on declarative relationships
Expand Down
4 changes: 3 additions & 1 deletion Libraries/Animated/AnimatedMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type {EndCallback} from './animations/Animation';
import type {TimingAnimationConfig} from './animations/TimingAnimation';
import type {DecayAnimationConfig} from './animations/DecayAnimation';
import type {SpringAnimationConfig} from './animations/SpringAnimation';

import type {Numeric as AnimatedNumeric} from './AnimatedImplementation';
import AnimatedColor from './nodes/AnimatedColor';

/**
Expand Down Expand Up @@ -163,6 +163,8 @@ const loop = function (
return emptyAnimation;
};

export type {AnimatedNumeric as Numeric};

module.exports = {
Value: AnimatedValue,
ValueXY: AnimatedValueXY,
Expand Down

0 comments on commit 9eb7629

Please sign in to comment.