[react-native] Fix declarations related to prop types e.g. requireNativeComponent (#28871)

Fixes `requireNativeComponent`, which was conflating the type of PropTypes maps with the type of prop maps. That is, these are not of the same type: `{ x: PropTypes.string }` and `{ x: "hello" }`.

Also fixes the type of the `ViewPropTypes` object. It is a map of PropTypes, not a single PropType. Additionally, made the types of the other PropType exports more precise instead of `PropType.any`.

Please fill in this template.

- [x] Use a meaningful title for the pull request. Include the name of the package modified.
- [x] Test the change in your own code. (Compile and run.)
- [x] Add or edit tests to reflect the change. (Run with `npm test`.)
- [x] Follow the advice from the [readme](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/README.md#make-a-pull-request).
- [x] Avoid [common mistakes](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/README.md#common-mistakes).
- [x] Run `npm run lint package-name` (or `tsc` if no `tslint.json` is present).

If changing an existing definition:
- [x] Provide a URL to documentation or source code which provides context for the suggested changes: **N/A** -- these are fixes
- [x] Increase the version number in the header if appropriate. **N/A**
- [x] If you are making substantial changes, consider adding a `tslint.json` containing `{ "extends": "dtslint/dt.json" }`. **N/A**
This commit is contained in:
James Ide 2018-09-14 09:10:16 -07:00 committed by Ryan Cavanaugh
parent b4143a7b47
commit 82a2833b4e
2 changed files with 19 additions and 8 deletions

View File

@ -31,6 +31,7 @@
/// <reference path="legacy-properties.d.ts" />
/// <reference path="BatchedBridge.d.ts" />
import * as PropTypes from 'prop-types';
import * as React from 'react';
type Constructor<T> = new(...args: any[]) => T;
@ -8936,7 +8937,7 @@ export const PixelRatio: PixelRatioStatic;
export interface ComponentInterface<P> {
name?: string;
displayName?: string;
propTypes: P;
propTypes: PropTypes.ValidationMap<P>;
}
/**
@ -8958,7 +8959,7 @@ export function requireNativeComponent<P>(
viewName: string,
componentInterface?: ComponentInterface<P>,
extraConfig?: { nativeOnly?: any }
): React.ComponentClass<P>;
): React.ComponentClass<PropTypes.InferProps<PropTypes.ValidationMap<P>>>;
export function findNodeHandle(
componentOrHandle: null | number | React.Component<any, any> | React.ComponentClass<any>
@ -9001,10 +9002,10 @@ export namespace addons {
//
// Prop Types
//
export const ColorPropType: React.Requireable<any>;
export const EdgeInsetsPropType: React.Requireable<any>;
export const PointPropType: React.Requireable<any>;
export const ViewPropTypes: React.Requireable<any>;
export const ColorPropType: React.Validator<string>;
export const EdgeInsetsPropType: React.Validator<Insets>;
export const PointPropType: React.Validator<PointPropType>;
export const ViewPropTypes: React.ValidationMap<ViewProps>;
declare global {
function require(name: string): any;

View File

@ -20,6 +20,7 @@ import {
BackAndroid,
BackHandler,
Button,
ColorPropType,
DataSourceAssetCallback,
DeviceEventEmitterStatic,
Dimensions,
@ -73,6 +74,7 @@ import {
KeyboardAvoidingView,
Modal,
TimePickerAndroid,
ViewPropTypes,
} from "react-native";
declare module "react-native" {
@ -186,13 +188,20 @@ const testNativeSyntheticEvent = <T extends {}>(e: NativeSyntheticEvent<T>): voi
e.nativeEvent;
}
type ElementProps<C> = C extends React.Component<infer P, any> ? P : never;
class CustomView extends React.Component {
render() {
return <Text style={[StyleSheet.absoluteFill, { ...StyleSheet.absoluteFillObject }]}>Custom View</Text>;
}
}
class Welcome extends React.Component {
class Welcome extends React.Component<ElementProps<View> & { color: string }> {
static propTypes = {
...ViewPropTypes,
color: ColorPropType,
};
refs: {
[key: string]: any;
rootView: View;
@ -218,8 +227,9 @@ class Welcome extends React.Component {
}
render() {
const { color, ...props } = this.props;
return (
<View ref="rootView" style={[[styles.container], undefined, null, false]}>
<View {...props} ref="rootView" style={[[styles.container], undefined, null, false]}>
<Text style={styles.welcome}>Welcome to React Native</Text>
<Text style={styles.instructions}>To get started, edit index.ios.js</Text>
<Text style={styles.instructions}>