Skip to content

Commit 44e7cd7

Browse files
committed
improve h() types according to Reacts allowed types
1 parent 4b9327b commit 44e7cd7

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/h.ts

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
import {createElement, ReactElement, ReactType} from 'react';
1+
import {
2+
createElement,
3+
ReactElement,
4+
ReactNode,
5+
ReactType,
6+
Attributes,
7+
} from 'react';
28
import {incorporate} from './incorporate';
39

410
export type PropsExtensions = {
511
sel?: string | symbol;
612
};
713

14+
type PropsLike<P> = P & PropsExtensions & Attributes;
15+
16+
type Children = string | Array<ReactNode>;
17+
818
function createElementSpreading<P = any>(
919
type: ReactType<P>,
10-
props: P | null,
11-
children: string | Array<ReactElement<any>>,
20+
props: PropsLike<P> | null,
21+
children: Children,
1222
): ReactElement<P> {
1323
if (typeof children === 'string') {
1424
return createElement(type, props, children);
@@ -19,7 +29,7 @@ function createElementSpreading<P = any>(
1929

2030
function hyperscriptProps<P = any>(
2131
type: ReactType<P>,
22-
props: P & PropsExtensions,
32+
props: PropsLike<P>,
2333
): ReactElement<P> {
2434
if (!props.sel) {
2535
return createElement(type, props);
@@ -30,15 +40,15 @@ function hyperscriptProps<P = any>(
3040

3141
function hyperscriptChildren<P = any>(
3242
type: ReactType<P>,
33-
children: string | Array<ReactElement<any>>,
43+
children: Children,
3444
): ReactElement<P> {
3545
return createElementSpreading(type, null, children);
3646
}
3747

3848
function hyperscriptPropsChildren<P = any>(
3949
type: ReactType<P>,
40-
props: P & PropsExtensions,
41-
children: string | Array<ReactElement<any>>,
50+
props: PropsLike<P>,
51+
children: Children,
4252
): ReactElement<P> {
4353
if (!props.sel) {
4454
return createElementSpreading(type, props, children);
@@ -49,14 +59,14 @@ function hyperscriptPropsChildren<P = any>(
4959

5060
export function h<P = any>(
5161
type: ReactType<P>,
52-
a?: (P & PropsExtensions) | string | Array<ReactElement<any>>,
53-
b?: string | Array<React.ReactElement<any>>,
62+
a?: PropsLike<P> | Children,
63+
b?: Children,
5464
): ReactElement<P> {
5565
if (a === undefined && b === undefined) {
5666
return createElement(type, null);
5767
}
5868
if (b === undefined && (typeof a === 'string' || Array.isArray(a))) {
59-
return hyperscriptChildren(type, a as string | Array<ReactElement<any>>);
69+
return hyperscriptChildren(type, a as Array<ReactNode>);
6070
}
6171
if (b === undefined && typeof a === 'object' && !Array.isArray(a)) {
6272
return hyperscriptProps(type, a);

0 commit comments

Comments
 (0)