Skip to content

Commit

Permalink
add some typings to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
chicoxyzzy committed Jun 24, 2016
1 parent 416b5ef commit a4e9871
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/docs/05-reusable-components.md
Expand Up @@ -213,7 +213,7 @@ class HelloMessage extends React.Component {
ReactDOM.render(<HelloMessage name="Sebastian" />, mountNode);
```

The API is similar to `React.createClass` with the exception of `getInitialState`. Instead of providing a separate `getInitialState` method, you set up your own `state` property in the constructor.
The API is similar to `React.createClass` with the exception of `getInitialState`. Instead of providing a separate `getInitialState` method, you set up your own `state` property in the constructor. Just like the return value of `getInitialState`, the value you assign to `this.state` will be used as the initial state for your component.

Another difference is that `propTypes` and `defaultProps` are defined as properties on the constructor instead of in the class body.

Expand Down
1 change: 1 addition & 0 deletions src/shared/utils/ReactFeatureFlags.js
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule ReactFeatureFlags
* @flow
*/

'use strict';
Expand Down
5 changes: 4 additions & 1 deletion src/shared/utils/ReactNodeTypes.js
Expand Up @@ -7,10 +7,13 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule ReactNodeTypes
* @flow
*/

'use strict';

type ReactNodeType = 0 | 1 | 2;

var ReactElement = require('ReactElement');

var invariant = require('invariant');
Expand All @@ -20,7 +23,7 @@ var ReactNodeTypes = {
COMPOSITE: 1,
EMPTY: 2,

getType: function(node) {
getType: function(node: ReactElement<any>): ReactNodeType {
if (node === null || node === false) {
return ReactNodeTypes.EMPTY;
} else if (ReactElement.isValidElement(node)) {
Expand Down
16 changes: 14 additions & 2 deletions src/shared/utils/deprecated.js
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule deprecated
* @flow
*/

'use strict';
Expand All @@ -24,7 +25,13 @@ var warning = require('warning');
* @param {function} fn The function to forward on to
* @return {function} The function that will warn once and then call fn
*/
function deprecated(fnName, newModule, newPackage, ctx, fn) {
function deprecated<T: Function>(
fnName: string,
newModule: string,
newPackage: string,
ctx: mixed,
fn: T,
): T {
var warned = false;
if (__DEV__) {
var newFn = function() {
Expand All @@ -47,7 +54,12 @@ function deprecated(fnName, newModule, newPackage, ctx, fn) {
};
// We need to make sure all properties of the original fn are copied over.
// In particular, this is needed to support PropTypes
return Object.assign(newFn, fn);
Object.assign(newFn, (fn: Object));

// Flow is not smart enough to figure out that newFn is of the same type as
// fn. Since we don't want to lose out the type of the function, casting
// to any and force flow to use T.
return ((newFn: any): T);
}

return fn;
Expand Down
40 changes: 24 additions & 16 deletions src/shared/utils/flattenChildren.js
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule flattenChildren
* @flow
*/

'use strict';
Expand All @@ -22,22 +23,29 @@ var warning = require('warning');
* @param {!string} name String name of key path to child.
* @param {number=} selfDebugID Optional debugID of the current internal instance.
*/
function flattenSingleChildIntoContext(traverseContext, child, name, selfDebugID) {
function flattenSingleChildIntoContext(
traverseContext: mixed,
child: ReactElement<any>,
name: string,
selfDebugID?: number
): void {
// We found a component instance.
var result = traverseContext;
var keyUnique = (result[name] === undefined);
if (__DEV__) {
warning(
keyUnique,
'flattenChildren(...): Encountered two children with the same key, ' +
'`%s`. Child keys must be unique; when two children share a key, only ' +
'the first child will be used.%s',
KeyEscapeUtils.unescape(name),
ReactComponentTreeDevtool.getStackAddendumByID(selfDebugID)
);
}
if (keyUnique && child != null) {
result[name] = child;
if (traverseContext && typeof traverseContext === 'object') {
const result = traverseContext;
const keyUnique = (result[name] === undefined);
if (__DEV__) {
warning(
keyUnique,
'flattenChildren(...): Encountered two children with the same key, ' +
'`%s`. Child keys must be unique; when two children share a key, only ' +
'the first child will be used.%s',
KeyEscapeUtils.unescape(name),
ReactComponentTreeDevtool.getStackAddendumByID(selfDebugID)
);
}
if (keyUnique && child != null) {
result[name] = child;
}
}
}

Expand All @@ -46,7 +54,7 @@ function flattenSingleChildIntoContext(traverseContext, child, name, selfDebugID
* children will not be included in the resulting object.
* @return {!object} flattened children keyed by name.
*/
function flattenChildren(children, selfDebugID) {
function flattenChildren(children: ReactElement<any>, selfDebugID?: number): ?{ [name: string]: ReactElement<any> } {
if (children == null) {
return children;
}
Expand Down
3 changes: 2 additions & 1 deletion src/shared/utils/getIteratorFn.js
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule getIteratorFn
* @flow
*/

'use strict';
Expand All @@ -29,7 +30,7 @@ var FAUX_ITERATOR_SYMBOL = '@@iterator'; // Before Symbol spec.
* @param {?object} maybeIterable
* @return {?function}
*/
function getIteratorFn(maybeIterable) {
function getIteratorFn(maybeIterable: ?any): ?(p: ReactElement<any>) => void {
var iteratorFn = maybeIterable && (
(ITERATOR_SYMBOL && maybeIterable[ITERATOR_SYMBOL]) ||
maybeIterable[FAUX_ITERATOR_SYMBOL]
Expand Down
5 changes: 3 additions & 2 deletions src/shared/utils/reactProdInvariant.js
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule reactProdInvariant
* @flow
*/
'use strict';

Expand All @@ -16,7 +17,7 @@
* and will _only_ be required by the corresponding babel pass.
* It always throws.
*/
function reactProdInvariant(code) {
function reactProdInvariant(code: string): void {
var argCount = arguments.length - 1;

var message = (
Expand All @@ -33,7 +34,7 @@ function reactProdInvariant(code) {
' for full errors and additional helpful warnings.'
);

var error = new Error(message);
var error: Error & { framesToPop?: number } = new Error(message);
error.name = 'Invariant Violation';
error.framesToPop = 1; // we don't care about reactProdInvariant's own frame

Expand Down

0 comments on commit a4e9871

Please sign in to comment.