react: Children fixes (#41760)

* `react`: `Children.map` fixes

https://reactjs.org/docs/react-api.html#reactchildren

* Refine return type

* Apply same changes to `toArray`
This commit is contained in:
Oliver Joseph Ash
2020-01-21 15:13:53 +00:00
committed by John Reilly
parent 903eaaf06d
commit 4fef8e2c33
2 changed files with 10 additions and 4 deletions
+4 -2
View File
@@ -2788,11 +2788,13 @@ declare namespace React {
// ----------------------------------------------------------------------
interface ReactChildren {
map<T, C>(children: C | C[], fn: (child: C, index: number) => T): T[];
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>>;
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;
toArray<C>(children: C | C[]): C[];
toArray(children: ReactNode | ReactNode[]): Array<Exclude<ReactNode, boolean | null | undefined>>;
}
//
+6 -2
View File
@@ -511,7 +511,7 @@ React.Children.forEach(children, (child) => { });
const nChildren: number = React.Children.count(children);
let onlyChild: React.ReactElement = React.Children.only(DOM.div()); // ok
onlyChild = React.Children.only([null, [[["Hallo"], true]], false]); // error
const childrenToArray: React.ReactChild[] = React.Children.toArray(children);
const childrenToArray: Array<Exclude<React.ReactNode, boolean | null | undefined>> = React.Children.toArray(children);
declare const numberChildren: number[];
declare const elementChildren: JSX.Element[];
@@ -519,13 +519,17 @@ declare const mixedChildren: Array<JSX.Element | string>;
declare const singlePluralChildren: JSX.Element | JSX.Element[];
declare const renderPropsChildren: () => JSX.Element;
// $ExpectType null
const mappedChildrenArray0 = React.Children.map(null, num => num);
// $ExpectType undefined
const mappedChildrenArray1 = React.Children.map(undefined, num => num);
// $ExpectType number[]
const mappedChildrenArray2 = React.Children.map(numberChildren, num => num);
// $ExpectType Element[]
const mappedChildrenArray3 = React.Children.map(elementChildren, element => element);
// $ExpectType (string | Element)[]
const mappedChildrenArray4 = React.Children.map(mixedChildren, elementOrString => elementOrString);
// $ExpectType (string | number | null)[]
// $ExpectType Key[]
const mappedChildrenArray5 = React.Children.map(singlePluralChildren, element => element.key);
// $ExpectType string[]
const mappedChildrenArray6 = React.Children.map(renderPropsChildren, element => element.name);