Skip to content
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

Refine StyleSheet Flow types #16741

Closed
wants to merge 3 commits into from
Closed

Conversation

jamesisaac
Copy link
Contributor

@jamesisaac jamesisaac commented Nov 8, 2017

Nice addition of the recent Flow types for style props in 9c29ee1, however I think there are some slight issues in the definition.

type Styles = {[key: string]: Object} makes sense, as it's referring to the set of named style groups a user creates a StyleSheet from, e.g.

const styles: StyleSheet = StyleSheet.create({
  container: {
    height: 20
  },
  text: {
    color: '#999',
    fontSize: 12,
  },
}: Styles)

However type StyleValue = {[key: string]: Object} doesn't make sense. You actually want only the Object portion of that definition, presuming it's meant to be used like below:

type MyTextProps = {
  style: StyleProp,
}

<MyText style={{ color: '#999', fontSize: 12 }}>Hello</Text>

I've also added void to the StyleValue, as undefined seems to be handled fine, and can be a useful shorthand in JSX.


And finally, I've allowed nesting of style prop arrays, by making StyleProp self-referencing, as RN seems to flatten those without issue. This can be important if you're passing in a style prop quite high up the component tree, and sticking it in an array with other styles at several points while it's passed down.

Test Plan

N/A (These types aren't referenced anywhere else)

Release Notes

[INTERNAL] [MINOR] [StyleSheet] - Refine Flow types

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 8, 2017
@jamesisaac jamesisaac changed the title Patch 1 Refine StyleSheet Flow types Nov 8, 2017
@frantic
Copy link
Contributor

frantic commented Nov 8, 2017

LGTM!

@@ -20,8 +20,8 @@ const flatten = require('flattenStyle');

export type Styles = {[key: string]: Object};
export type StyleSheet<S: Styles> = {[key: $Keys<S>]: number};
export type StyleValue = {[key: string]: Object} | number | false | null;
export type StyleProp = StyleValue | Array<StyleValue>;
export type StyleValue = Object | number | false | null | void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to flattenStyle any "falsy" value works so the users could do thing && styles.blah. I'm not sure if Flow has a special type representing falsy values, but might be a good idea to also include '' (empty string) here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot, thanks. I don't know of any special falsey type in Flow, so have just gone with empty string.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheSavior has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheSavior has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@elicwhite
Copy link
Member

Thanks for contributing!

cdlewis pushed a commit to cdlewis/react-native that referenced this pull request Nov 19, 2017
Summary:
Nice addition of the recent Flow types for style props in 9c29ee1, however I think there are some slight issues in the definition.

`type Styles = {[key: string]: Object}` makes sense, as it's referring to the set of named style groups a user creates a `StyleSheet` from, e.g.

```javascript
const styles: StyleSheet = StyleSheet.create({
  container: {
    height: 20
  },
  text: {
    color: 'facebook#999',
    fontSize: 12,
  },
}: Styles)
```

However `type StyleValue = {[key: string]: Object}` doesn't make sense.  You actually want only the `Object` portion of that definition, presuming it's meant to be used like below:

```javascript
type MyTextProps = {
  style: StyleProp,
}

<MyText style={{ color: 'facebook#999', fontSize: 12 }}>Hello</Text>
```

 ---

I've also added `void` to the `StyleValue`, as undefined seems to be handled fine, and can be a useful shorthand in JSX.

 ---

And finally, I've allowed nesting of style prop arrays, by making StyleProp self-referencing, as RN seems to flatten those without issue.  This can be important if you're passing in a style prop quite high up the component tree, and sticking it in an array with other styles at several points while it's passed down.

N/A (These types aren't referenced anywhere else)

[INTERNAL] [MINOR] [StyleSheet] - Refine Flow types
Closes facebook#16741

Reviewed By: frantic

Differential Revision: D6278010

Pulled By: TheSavior

fbshipit-source-id: 0170a233ab71d29f445786f5e695463f9730db3a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants