react: Children.map: replace overload with conditional type (#41771)

* `react`: `Children.map`: replace overload with conditional type

* Document
This commit is contained in:
Oliver Joseph Ash 2020-01-22 17:27:29 +00:00 committed by Ben Lichtman
parent b406bc6d23
commit a09aa0ec09
2 changed files with 6 additions and 3 deletions

View File

@ -2788,9 +2788,8 @@ declare namespace React {
// ----------------------------------------------------------------------
interface ReactChildren {
map(children: null, fn: (child: never, index: number) => never): null;
map(children: undefined, fn: (child: never, index: number) => never): undefined;
map<T, C>(children: C | C[], fn: (child: C, index: number) => T): Array<Exclude<T, boolean | null | undefined>>;
map<T, C>(children: C | C[], fn: (child: C, index: number) => T):
C extends null | undefined ? C : Array<Exclude<T, boolean | null | undefined>>;
forEach<C>(children: C | C[], fn: (child: C, index: number) => void): void;
count(children: any): number;
only<C>(children: C): C extends any[] ? never : C;

View File

@ -514,6 +514,7 @@ onlyChild = React.Children.only([null, [[["Hallo"], true]], false]); // error
const childrenToArray: Array<Exclude<React.ReactNode, boolean | null | undefined>> = React.Children.toArray(children);
declare const numberChildren: number[];
declare const nodeChildren: React.ReactNode;
declare const elementChildren: JSX.Element[];
declare const mixedChildren: Array<JSX.Element | string>;
declare const singlePluralChildren: JSX.Element | JSX.Element[];
@ -533,6 +534,9 @@ const mappedChildrenArray4 = React.Children.map(mixedChildren, elementOrString =
const mappedChildrenArray5 = React.Children.map(singlePluralChildren, element => element.key);
// $ExpectType string[]
const mappedChildrenArray6 = React.Children.map(renderPropsChildren, element => element.name);
// The return type may not be an array
// $ExpectError
const mappedChildrenArray7 = React.Children.map(nodeChildren, node => node).map;
//
// Example from http://facebook.github.io/react/