1
- import { createElement , ReactElement , ReactType } from 'react' ;
1
+ import {
2
+ createElement ,
3
+ ReactElement ,
4
+ ReactNode ,
5
+ ReactType ,
6
+ Attributes ,
7
+ } from 'react' ;
2
8
import { incorporate } from './incorporate' ;
3
9
4
10
export type PropsExtensions = {
5
11
sel ?: string | symbol ;
6
12
} ;
7
13
14
+ type PropsLike < P > = P & PropsExtensions & Attributes ;
15
+
16
+ type Children = string | Array < ReactNode > ;
17
+
8
18
function createElementSpreading < P = any > (
9
19
type : ReactType < P > ,
10
- props : P | null ,
11
- children : string | Array < ReactElement < any > > ,
20
+ props : PropsLike < P > | null ,
21
+ children : Children ,
12
22
) : ReactElement < P > {
13
23
if ( typeof children === 'string' ) {
14
24
return createElement ( type , props , children ) ;
@@ -19,7 +29,7 @@ function createElementSpreading<P = any>(
19
29
20
30
function hyperscriptProps < P = any > (
21
31
type : ReactType < P > ,
22
- props : P & PropsExtensions ,
32
+ props : PropsLike < P > ,
23
33
) : ReactElement < P > {
24
34
if ( ! props . sel ) {
25
35
return createElement ( type , props ) ;
@@ -30,15 +40,15 @@ function hyperscriptProps<P = any>(
30
40
31
41
function hyperscriptChildren < P = any > (
32
42
type : ReactType < P > ,
33
- children : string | Array < ReactElement < any > > ,
43
+ children : Children ,
34
44
) : ReactElement < P > {
35
45
return createElementSpreading ( type , null , children ) ;
36
46
}
37
47
38
48
function hyperscriptPropsChildren < P = any > (
39
49
type : ReactType < P > ,
40
- props : P & PropsExtensions ,
41
- children : string | Array < ReactElement < any > > ,
50
+ props : PropsLike < P > ,
51
+ children : Children ,
42
52
) : ReactElement < P > {
43
53
if ( ! props . sel ) {
44
54
return createElementSpreading ( type , props , children ) ;
@@ -49,14 +59,14 @@ function hyperscriptPropsChildren<P = any>(
49
59
50
60
export function h < P = any > (
51
61
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 ,
54
64
) : ReactElement < P > {
55
65
if ( a === undefined && b === undefined ) {
56
66
return createElement ( type , null ) ;
57
67
}
58
68
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 > ) ;
60
70
}
61
71
if ( b === undefined && typeof a === 'object' && ! Array . isArray ( a ) ) {
62
72
return hyperscriptProps ( type , a ) ;
0 commit comments