Skip to content

Commit 401082d

Browse files
authored
Fix types of automatic and classic runtimes
Closes GH-10. Closes GH-11. Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com> Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 3f8476b commit 401082d

File tree

5 files changed

+48
-49
lines changed

5 files changed

+48
-49
lines changed

lib/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
* @typedef {string|number|null|undefined} XPrimitiveChild
1111
* @typedef {Array.<Node|XPrimitiveChild>} XArrayChild
1212
* @typedef {Node|XPrimitiveChild|XArrayChild} XChild
13+
* @typedef {import('./jsx-classic').Element} x.JSX.Element
14+
* @typedef {import('./jsx-classic').IntrinsicAttributes} x.JSX.IntrinsicAttributes
15+
* @typedef {import('./jsx-classic').IntrinsicElements} x.JSX.IntrinsicElements
16+
* @typedef {import('./jsx-classic').ElementChildrenAttribute} x.JSX.ElementChildrenAttribute
1317
*/
1418

15-
export * from './jsx-classic.js'
16-
1719
/**
1820
* Create XML trees in xast.
1921
*

lib/jsx-automatic.d.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import {XAttributes, XChild, XResult} from './index.js'
22

3-
/**
4-
* This unique symbol is declared to specify the key on which JSX children are passed, without conflicting
5-
* with the Attributes type.
6-
*/
7-
declare const children: unique symbol
8-
93
export namespace JSX {
104
/**
115
* This defines the return value of JSX syntax.
@@ -32,7 +26,7 @@ export namespace JSX {
3226
/**
3327
* The prop that matches `ElementChildrenAttribute` key defines the type of JSX children, defines the children type.
3428
*/
35-
[children]?: XChild
29+
children?: XChild
3630
}
3731
}
3832

@@ -43,6 +37,6 @@ export namespace JSX {
4337
/**
4438
* Only the key matters, not the value.
4539
*/
46-
[children]?: never
40+
children?: never
4741
}
4842
}

lib/jsx-classic.d.ts

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,46 @@
1-
import {XAttributes, XChild, XResult, x} from './index.js'
1+
import {XAttributes, XChild, XResult} from './index.js'
22

33
/**
44
* This unique symbol is declared to specify the key on which JSX children are passed, without conflicting
55
* with the Attributes type.
66
*/
77
declare const children: unique symbol
88

9-
export namespace JSX {
10-
/**
11-
* This defines the return value of JSX syntax.
12-
*/
13-
type Element = XResult
9+
/**
10+
* This defines the return value of JSX syntax.
11+
*/
12+
export type Element = XResult
1413

15-
/**
16-
* This disallows the use of functional components.
17-
*/
18-
type IntrinsicAttributes = never
14+
/**
15+
* This disallows the use of functional components.
16+
*/
17+
export type IntrinsicAttributes = never
1918

20-
/**
21-
* This defines the prop types for known elements.
22-
*
23-
* For `xastscript` this defines any string may be used in combination with `xast` `Attributes`.
24-
*
25-
* This **must** be an interface.
26-
*/
27-
// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
28-
interface IntrinsicElements {
29-
[name: string]:
30-
| XAttributes
31-
| {
32-
/**
33-
* The prop that matches `ElementChildrenAttribute` key defines the type of JSX children, defines the children type.
34-
*/
35-
[children]?: XChild
36-
}
37-
}
19+
/**
20+
* This defines the prop types for known elements.
21+
*
22+
* For `xastscript` this defines any string may be used in combination with `xast` `Attributes`.
23+
*
24+
* This **must** be an interface.
25+
*/
26+
// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
27+
export interface IntrinsicElements {
28+
[name: string]:
29+
| XAttributes
30+
| {
31+
/**
32+
* The prop that matches `ElementChildrenAttribute` key defines the type of JSX children, defines the children type.
33+
*/
34+
[children]?: XChild
35+
}
36+
}
3837

38+
/**
39+
* The key of this interface defines as what prop children are passed.
40+
*/
41+
export interface ElementChildrenAttribute {
3942
/**
40-
* The key of this interface defines as what prop children are passed.
43+
* Only the key matters, not the value.
4144
*/
42-
interface ElementChildrenAttribute {
43-
/**
44-
* Only the key matters, not the value.
45-
*/
46-
[children]?: never
47-
}
45+
[children]?: never
4846
}

test-d/automatic.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,9 @@ expectError(<a invalid={{}} />)
4949
expectError(<a invalid={[1]} />)
5050
expectError(<a>{{invalid: 'child'}}</a>)
5151

52+
// This is where the automatic runtime differs from the classic runtime.
53+
// The automatic runtime the children prop to define JSX children, whereas it’s used as an attribute in the classic runtime.
54+
expectType<Result>(<a children={<b />} />)
55+
5256
declare function Bar(props?: Record<string, unknown>): Element
5357
expectError(<Bar />)

test-d/classic.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import {x} from '../index.js'
66

77
type Result = Element | Root
88

9-
// To do: fix classic types.
10-
/* eslint-disable @typescript-eslint/no-unsafe-argument */
119
expectType<Result>(<></>)
1210
expectType<Result>(<a />)
1311
expectType<Result>(<a b="c" />)
@@ -40,7 +38,10 @@ expectError(<a invalid={{}} />)
4038
expectError(<a invalid={[1]} />)
4139
expectError(<a>{{invalid: 'child'}}</a>)
4240

41+
// This is where the classic runtime differs from the automatic runtime.
42+
// The automatic runtime the children prop to define JSX children, whereas it’s
43+
// used as an attribute in the classic runtime.
44+
expectError(<a children={<b />} />)
45+
4346
declare function Bar(props?: Record<string, unknown>): Element
4447
expectError(<Bar />)
45-
46-
/* eslint-enable @typescript-eslint/no-unsafe-argument */

0 commit comments

Comments
 (0)