From 82a2833b4e34f9280d758efa2cfbe35ec6ec8b71 Mon Sep 17 00:00:00 2001 From: James Ide Date: Fri, 14 Sep 2018 09:10:16 -0700 Subject: [PATCH] [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** --- types/react-native/index.d.ts | 13 +++++++------ types/react-native/test/index.tsx | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/types/react-native/index.d.ts b/types/react-native/index.d.ts index ad8abe57fd..98f6c92c3f 100644 --- a/types/react-native/index.d.ts +++ b/types/react-native/index.d.ts @@ -31,6 +31,7 @@ /// /// +import * as PropTypes from 'prop-types'; import * as React from 'react'; type Constructor = new(...args: any[]) => T; @@ -8936,7 +8937,7 @@ export const PixelRatio: PixelRatioStatic; export interface ComponentInterface

{ name?: string; displayName?: string; - propTypes: P; + propTypes: PropTypes.ValidationMap

; } /** @@ -8958,7 +8959,7 @@ export function requireNativeComponent

( viewName: string, componentInterface?: ComponentInterface

, extraConfig?: { nativeOnly?: any } -): React.ComponentClass

; +): React.ComponentClass>>; export function findNodeHandle( componentOrHandle: null | number | React.Component | React.ComponentClass @@ -9001,10 +9002,10 @@ export namespace addons { // // Prop Types // -export const ColorPropType: React.Requireable; -export const EdgeInsetsPropType: React.Requireable; -export const PointPropType: React.Requireable; -export const ViewPropTypes: React.Requireable; +export const ColorPropType: React.Validator; +export const EdgeInsetsPropType: React.Validator; +export const PointPropType: React.Validator; +export const ViewPropTypes: React.ValidationMap; declare global { function require(name: string): any; diff --git a/types/react-native/test/index.tsx b/types/react-native/test/index.tsx index 8bc6757c48..b2b66ee2da 100644 --- a/types/react-native/test/index.tsx +++ b/types/react-native/test/index.tsx @@ -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 = (e: NativeSyntheticEvent): voi e.nativeEvent; } +type ElementProps = C extends React.Component ? P : never; + class CustomView extends React.Component { render() { return Custom View; } } -class Welcome extends React.Component { +class Welcome extends React.Component & { 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 ( - + Welcome to React Native To get started, edit index.ios.js