3232 * List of children.
3333 * @typedef {Node | HPrimitiveChild | HArrayChild } HChild
3434 * Acceptable child value.
35+ * @typedef {Record<string, unknown> } HComponentProperties
36+ * Acceptable properties for a component function.
37+ * @typedef {(props: HComponentProperties) => HResult } HComponent
38+ * Function returning a hyperscript result (useful for JSX)
3539 */
3640
3741import { find , normalize } from 'property-information'
@@ -58,18 +62,38 @@ export function core(schema, defaultTagName, caseSensitive) {
5862 * (selector: null | undefined, ...children: Array<HChild>): Root
5963 * (selector: string, properties: HProperties, ...children: Array<HChild>): Element
6064 * (selector: string, ...children: Array<HChild>): Element
65+ * (component: HComponent, props: HComponentProperties, ...children: Array<HChild>): HResult
6166 * }}
6267 */
6368 (
6469 /**
6570 * Hyperscript compatible DSL for creating virtual hast trees.
6671 *
67- * @param {string | null | undefined } [selector]
68- * @param {HProperties | HChild | null | undefined } [properties]
72+ * @param {string | null | undefined | HComponent } [selector]
73+ * @param {HProperties | HChild | null | undefined | HComponentProperties } [properties]
6974 * @param {Array<HChild> } children
7075 * @returns {HResult }
7176 */
7277 function ( selector , properties , ...children ) {
78+ if ( typeof selector === 'function' ) {
79+ const props = properties ?? { }
80+ if ( typeof props !== 'object' ) {
81+ // We should rarely hit this case because this codepath is only
82+ // called by JSX transforms, but we have to make the check to
83+ // satisfy TypeScript.
84+ throw new TypeError (
85+ `second argument to h(${
86+ selector . name
87+ } , props) must be an object, but was ${ typeof props } `
88+ )
89+ }
90+
91+ return selector ( {
92+ ...props ,
93+ children
94+ } )
95+ }
96+
7397 let index = - 1
7498 /** @type {HResult } */
7599 let node
@@ -98,6 +122,7 @@ export function core(schema, defaultTagName, caseSensitive) {
98122 }
99123 }
100124 } else {
125+ // @ts -expect-error cannot be `HComponentProperties` because we're not on that codepath.
101126 children . unshift ( properties )
102127 }
103128 }
@@ -120,7 +145,7 @@ export function core(schema, defaultTagName, caseSensitive) {
120145}
121146
122147/**
123- * @param {HProperties | HChild } value
148+ * @param {HProperties | HChild | HComponentProperties } value
124149 * @param {string } name
125150 * @returns {value is HProperties }
126151 */
0 commit comments