-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
First pass adding flow annotations. #187
Conversation
Awesome! |
@@ -1,4 +1,6 @@ | |||
export default function hasReachedStyle(currentStyle, style) { | |||
/* @flow */ | |||
export default function hasReachedStyle(currentStyle: Object, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible theoretically to type check this as something more precise than Object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That’s possible. What additional constraints would you like to enforce?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Humm on second thought it could be a bit complicated. The thing is, currentStyle
looks like this: {height: {val: 10, config: [1, 2]}, width: justANumber, randomKey: someRandomValue}
. The randomKey
part screws things up. It's needed because for TransitionMotion you might need to carry around a pack of info around so that you can still access them when the item's gone everywhere else. Do you know what I mean?
If this is type-checkable it'd be great. Say it's type A. TransitionMotion's styles
would be typed as a map of {stringKey: A}
. Avoids some confusion if you use types everywhere.
So right now it's not easily typeable. 2 solutions I can think of:
- Impose that recognized keys, e.g.
height
,transform
,opacity
, etc. Have that Number | ConfigObject type. This isn't cross-platform though. - Impose that anything you carry around be in a key called
data
, of typeAny
. Everything else should be Number | ConfigObject.
Any other solution? If not, either way we'd need to leave this as Object
for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I don’t understand why it isn’t cross-platform. Because you would need to hardcode all the possible keys? But then we would have to track all possible CSS properties. I do not think we should enforce the validity of the CSS property at the type level.
- I prefer this solution. But couldn’t
randomKey
become an extra parameter instead of living inside the same configuration object. We would have a config that includes the essentials that every animation needs, and could be precisley type-checked and a separate object for random stuff. Right now the situation is a bit contradictory as the code operates on the style objects with functions that are extremely generic and would almost work for any type of object, yet we also wish to impose constraints on those same objects. 'Isolate the parts that change from those that don’t' might also apply to types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Well partially, yes. This also has to do with the fact that it's not clear that you'd want height rounded to integer on e.g. react-canvas (if they even care about react-motion).
- The extra data carried around is only for TransitionMotion. How would you do this:
<TransitionMotion styles={stuff}>{interpolated => Object.keys(interpolated).map(style => randomKeyWithMyData...)}
Passing as an extra param would work well here, would it?
cc @phaistonian
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now it could be a simple type Options
which equals to Object
. Later Options
can be updated with more specific type. And it will be in one place rather then updating (and possibly missing some) all places in the code that use the same Options
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chenglou I've first read your commend in the other way and that was funny.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ve had mixed experiences with type aliases for simple types in Scala. I guess it would not change much whether we define a new Options type in this case. Do you know whether we can export type definitions from a module like any other variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes you can. Also, I'm ambivalent as to aliasing it to something else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really think we need alias. It is not about Options being equal to Object, it is about idiomatic function API.
It also gives you ability to do compile-time checks if arguments are actually the same type. So if in one place you will use Options and in the other place Object - check will fail.
Often I have separate PropTypes file that exports something like:
export const id = React.PropTypes.number;
export const name = React.PropTypes.string;
Humm, I guess it's all set? |
const errorMargin = 0.0001; | ||
|
||
export default function stepper(frameRate, x, v, destX, k, b) { | ||
export default function stepper(frameRate: number, | ||
x: number, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for nitpicking but I can't recall us using this awkward indentation and it will not pass ESLint check.
Should be something like
export default function stepper(
frameRate: number,
x: number,
v: number,
destX: number,
k: number,
b: number): [number, number] {
Disregard if I'm wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does pass ESLint checks (you might want to change the ESLint config), but I can change it anyway. Is it just the first argument on a new line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is 2 space indentation, and first arg on a new line too.
Thanks.
So I guess this is good to go? Beside the nits =D |
I have fixed the indentation issue that @nkbt highlighted and created a Style type. Now that Flow 0.17 is out we can try to check the rest of the code base. Do you want to merge this one right now or do you prefer if I try to annotate more files first? |
Interestingly, if you update to the latest ESLint and AirBnB style guides, the |
|
You get lint errors only if you change the |
Alright, merging! =D Thanks a lot! |
First pass adding flow annotations.
import type {Style} from './Types.js'; | ||
|
||
export default function hasReachedStyle(currentStyle: Style, | ||
style: Style): boolean { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was expecting this indentation to be fixed everywhere not only in the place I left my comment ;)
Sorry for post-review, I somehow missed updates |
As @chenglou suggested in #180 , here’s a pull request with flow checks added to just a subset of the project files. I am only checking files where Flow recognizes all language constructs. Since Flow already merged the fixes related to
const
destructuring andexport default
, it seems pointless to change the project coding style, only to go back when the next Flow version releases.Among the project dependencies, I ignored some files because we hit facebook/fbjs#44 with React 0.14.0-rc1; I also ignored a dummy
package.json
file used to test npmconf because flow mistakes it for the real deal.