Skip to content

Commit 15c010e

Browse files
committed
Better JSX types.
1 parent 7bd59a3 commit 15c010e

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

site/public/guides/enabling-jsx.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,22 @@ declare namespace JSX {
141141
For HTML tag autocompletion and good-enough attr validation, add this:
142142

143143
```ts
144-
type jsxify<T extends HTMLElement> = {
145-
[A in keyof T as A extends string ? Lowercase<Exclude<A, 'children'>> : never]?:
146-
string | boolean |
147-
(T[A] extends (string | boolean | null | number)
148-
? T[A]
149-
: never)
144+
declare namespace JSX {
145+
146+
// ...
150147

148+
type jsxify<T extends HTMLElement> = {
149+
[A in keyof T as A extends string ? Lowercase<Exclude<A, 'children'>> : never]?: (
150+
| string
151+
| boolean
152+
| (T[A] extends (string | boolean | null | number)
153+
? T[A]
154+
: never))
151155
} & { children?: any, class?: string }
152156

153157
type IntrinsicElements =
154158
& { [K in keyof HTMLElementTagNameMap]: jsxify<HTMLElementTagNameMap[K]> }
155-
// add special cases here as necessary like this:
156159
& { meta: jsxify<HTMLMetaElement> & { charset?: 'utf-8' } }
160+
161+
}
157162
```

types.d.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
declare namespace JSX {
22

33
type jsxify<T extends HTMLElement> = {
4-
[A in keyof T as A extends string ? Lowercase<Exclude<A, 'children'>> : never]?:
5-
string | boolean |
6-
(T[A] extends (string | boolean | null | number)
7-
? T[A]
8-
: never)
9-
4+
[A in keyof T as A extends string ? Lowercase<Exclude<A, 'children'>> : never]?: (
5+
| string
6+
| boolean
7+
| (T[A] extends (string | boolean | null | number)
8+
? T[A]
9+
: never))
1010
} & { children?: any, class?: string }
1111

1212
type IntrinsicElements =
1313
& { [K in keyof HTMLElementTagNameMap]: jsxify<HTMLElementTagNameMap[K]> }
1414
// add special cases here as necessary like this:
1515
& { meta: jsxify<HTMLMetaElement> & { charset?: 'utf-8' } }
1616

17-
type ElementChildrenAttribute = { children: any }
17+
type jsxChildren =
18+
| string
19+
| false
20+
| null
21+
| undefined
22+
| jsxChildren[]
23+
24+
type ElementChildrenAttribute = { children: jsxChildren }
1825

1926
type Element = string
2027

2128
type ElementType =
2229
| string
23-
| ((data: any) => JSX.Element)
30+
| ((data: any) => jsxChildren)
2431

2532
}
2633

0 commit comments

Comments
 (0)