Skip to content

Conversation

samwgoldman
Copy link
Member

Implemented func, object, and objectOf. Added tests for the existing arrayOf proptype. "Fixed" issue flowing React props to generated proptypes (see ba95d17).

This proptype translates to the same thing as the "function" and
"Function" types, that is, any function.

I extracted a helper to create this type, as it is used in two places.
This proptype works exactly like the "Object" type annotation, accepting
any object.
@samwgoldman samwgoldman changed the title More proptypes2 Implement PropTypes.func and PropTypes.object, test PropTypes.arrayOf Mar 10, 2015
When props flow into react element proptypes, they are not treated as
object literals, because their reason is not exactly "object literal."
This causes the props to be unified with the proptypes instead of
compared using subtyping rules.

The real fix to this is to carry around "this is literal" information
separately from the reason string, but until we have that, I'm making
this hack even dirtier.

This has a nice benefit of simplifying type failures, as you can see in
the changed test output. I verified that all removed error messages are
inverses of messages that still exist -- i.e., we have not lost any type
checking.
@samwgoldman samwgoldman changed the title Implement PropTypes.func and PropTypes.object, test PropTypes.arrayOf Implement more PropTypes Mar 11, 2015
let lit = (desc_of_reason reason1) = "object literal" in
let lit =
let desc = (desc_of_reason reason1) in
let is_prefix a b =
Copy link
Contributor

Choose a reason for hiding this comment

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

Here's a simpler is_prefix:

let is_prefix a b = String.(
  let n = length b in 
  length a >= n && sub a 0 n = b)

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 I'll use that instead.

@bhosmer
Copy link
Contributor

bhosmer commented Mar 11, 2015

Awesome... hopefully the march of progress will sweep away the reason hacking in short order. :) Meanwhile, if you could substitute the simpler is_prefix (or an equivalent) I'll merge this baby in. Thanks again!

@samwgoldman
Copy link
Member Author

Should be good to go. Only a few simple proptypes left.

@@ -47,10 +47,6 @@ new_react.js:4:12,33: string
This type is incompatible with
new_react.js:18:15,20: number

new_react.js:4:12,33: string
Copy link
Contributor

Choose a reason for hiding this comment

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

Any idea why this goes away?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I mentioned this in the commit message for this change. Since we used to unify props, proptypes were checked in both directions. Elsewhere in this expectation the same error exists with the line numbers reversed.

@bhosmer
Copy link
Contributor

bhosmer commented Mar 11, 2015

Hi Sam - everything we talked about looks great. The error that gets dropped is a puzzle though... apologies for not noting it earlier. Any idea what's going on there?

@samwgoldman
Copy link
Member Author

@bhosmer, yup, it's invariance vs subtyping. The error message used to fail "both ways" and now just fails one way. Intermediate states of this PR (before the proptype literal reason hacks) had a lot more of those "duplicated" errors. I think it's nicer now, and shouldn't entail a loss of type checking.

@bhosmer
Copy link
Contributor

bhosmer commented Mar 11, 2015

Ah, cool - missed the inverted one later in the error dump. Thanks!

bhosmer added a commit that referenced this pull request Mar 11, 2015
@bhosmer bhosmer merged commit a3b1d2c into facebook:master Mar 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants