Skip to content

Commit ae638ba

Browse files
committed
Add docs on global JSX namespace
1 parent 929ea49 commit ae638ba

File tree

5 files changed

+88
-4
lines changed

5 files changed

+88
-4
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ yarn.lock
99
/example/hast-util-to-jsx-runtime.min.js
1010
!/lib/types.d.ts
1111
!/index.d.ts
12-
!/types.d.ts
12+
!/types-esmsh.d.ts
13+
!/types-jsx.d.ts

readme.md

+66-2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@ Yields:
108108
<h1>Hello, world!</h1>
109109
```
110110

111+
> **Note**:
112+
> To add proper type support in React\@19,
113+
> register a global JSX namespace:
114+
>
115+
> ```ts
116+
> import type {JSX as Jsx} from 'react/jsx-runtime'
117+
>
118+
> declare global {
119+
> namespace JSX {
120+
> type ElementClass = Jsx.ElementClass
121+
> type Element = Jsx.Element
122+
> type IntrinsicElements = Jsx.IntrinsicElements
123+
> }
124+
> }
125+
> ```
126+
111127
## API
112128
113129
This package exports the identifier [`toJsxRuntime`][api-to-jsx-runtime].
@@ -517,6 +533,21 @@ render(
517533
)
518534
```
519535

536+
To add proper type support,
537+
register a global JSX namespace:
538+
539+
```ts
540+
import type {JSX as Jsx} from 'preact/jsx-runtime'
541+
542+
declare global {
543+
namespace JSX {
544+
type ElementClass = Jsx.ElementClass
545+
type Element = Jsx.Element
546+
type IntrinsicElements = Jsx.IntrinsicElements
547+
}
548+
}
549+
```
550+
520551
### Example: Solid
521552

522553
> 👉 **Note**: you must set `elementAttributeNameCase: 'html'` and
@@ -567,6 +598,21 @@ function Component() {
567598
}
568599
```
569600

601+
To add proper type support,
602+
register a global JSX namespace:
603+
604+
```ts
605+
import type {JSX as Jsx} from 'solid-js/jsx-runtime'
606+
607+
declare global {
608+
namespace JSX {
609+
type ElementClass = Jsx.ElementClass
610+
type Element = Jsx.Element
611+
type IntrinsicElements = Jsx.IntrinsicElements
612+
}
613+
}
614+
```
615+
570616
### Example: Svelte
571617

572618
<!-- To do: improve svelte when it fixes a bunch of bugs. -->
@@ -590,6 +636,9 @@ Yields:
590636
[class Component extends SvelteComponent]
591637
```
592638

639+
Types for Svelte are broken.
640+
Raise it with Svelte.
641+
593642
### Example: Vue
594643

595644
> 👉 **Note**: you must set `elementAttributeNameCase: 'html'` for Vue.
@@ -600,7 +649,7 @@ In Node.js, do:
600649
import serverRenderer from '@vue/server-renderer'
601650
import {h} from 'hastscript'
602651
import {toJsxRuntime} from 'hast-util-to-jsx-runtime'
603-
import {Fragment, jsx, jsxs} from 'vue/jsx-runtime' // Available since `vue@^3.3.0-alpha.6`.
652+
import {Fragment, jsx, jsxs} from 'vue/jsx-runtime' // Available since `[email protected]`.
604653

605654
console.log(
606655
await serverRenderer.renderToString(
@@ -640,6 +689,21 @@ function Component() {
640689
}
641690
```
642691

692+
To add proper type support,
693+
register a global JSX namespace:
694+
695+
```ts
696+
import type {JSX as Jsx} from 'vue/jsx-runtime'
697+
698+
declare global {
699+
namespace JSX {
700+
type ElementClass = Jsx.ElementClass
701+
type Element = Jsx.Element
702+
type IntrinsicElements = Jsx.IntrinsicElements
703+
}
704+
}
705+
```
706+
643707
## Syntax
644708

645709
HTML is parsed according to WHATWG HTML (the living standard), which is also
@@ -653,7 +717,7 @@ versions of Node.js.
653717
When we cut a new major release, we drop support for unmaintained versions of
654718
Node.
655719
This means we try to keep the current release line,
656-
`hast-util-to-jsx-runtime@^2`, compatible with Node.js 16.
720+
`hast-util-to-jsx-runtime@2`, compatible with Node.js 16.
657721

658722
## Security
659723

tsconfig.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,11 @@
1212
"target": "es2022"
1313
},
1414
"exclude": ["coverage/", "node_modules/"],
15-
"include": ["**/**.js", "lib/types.d.ts", "index.d.ts", "types.d.ts"]
15+
"include": [
16+
"**/**.js",
17+
"lib/types.d.ts",
18+
"index.d.ts",
19+
"types-esmsh.d.ts",
20+
"types-jsx.d.ts"
21+
]
1622
}
File renamed without changes.

types-jsx.d.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// To do: this is needed for React 19.
2+
// See <https://github.com/DefinitelyTyped/DefinitelyTyped/pull/69142/commits/ca06234>
3+
// for more info.
4+
5+
// import type {JSX as Jsx} from 'react/jsx-runtime'
6+
7+
// declare global {
8+
// namespace JSX {
9+
// type ElementClass = Jsx.ElementClass
10+
// type Element = Jsx.Element
11+
// type IntrinsicElements = Jsx.IntrinsicElements
12+
// }
13+
// }

0 commit comments

Comments
 (0)