Skip to content

Commit 8696942

Browse files
types: support xastscript used as JSX in typescript
Co-authored-by: Remco Haszing <remcohaszing@gmail.com>
1 parent 5311151 commit 8696942

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed

types/index.d.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// TypeScript Version: 3.7
22

3-
import {Element, Node, Root} from 'xast'
3+
import {Element as XastElement, Node, Root} from 'xast'
44

55
type Children = string | Node | number | Children[]
66

@@ -17,7 +17,7 @@ type Attributes = Record<string, Primitive>
1717
* @param name Qualified name. Case sensitive and can contain a namespace prefix (such as rdf:RDF).
1818
* @param children (Lists of) child nodes. When strings are encountered, they are mapped to Text nodes.
1919
*/
20-
declare function xastscript(name: string, ...children: Children[]): Element
20+
declare function xastscript(name: string, ...children: Children[]): XastElement
2121

2222
/**
2323
* Create XML trees in xast.
@@ -38,6 +38,37 @@ declare function xastscript(
3838
name: string,
3939
attributes?: Attributes,
4040
...children: Children[]
41-
): Element
41+
): XastElement
42+
43+
declare namespace xastscript.JSX {
44+
/**
45+
* This defines the return value of JSX syntax.
46+
*/
47+
type Element = XastElement
48+
49+
/**
50+
* This disallows the use of
51+
*/
52+
type IntrinsicAttributes = never
53+
54+
/**
55+
* This defines the prop types for known elements.
56+
*
57+
* For `xastscript` this defines any string may be used in combination with `xast` `Attributes`.
58+
*/
59+
interface IntrinsicElements {
60+
[element: string]: Attributes
61+
}
62+
63+
/**
64+
* The key of this interface defines as what prop children are passed.
65+
*/
66+
interface ElementChildrenAttribute {
67+
/**
68+
* Only the key matters, not the value.
69+
*/
70+
'': never
71+
}
72+
}
4273

4374
export = xastscript

types/test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import x = require('xastscript')
22

3+
const xmlns = 'http://www.sitemaps.org/schemas/sitemap/0.9'
4+
const xmlNumberAttribute = 100
5+
36
x('urlset') // $ExpectType Element
47
x('urlset', 'string') // $ExpectType Element
58
x('urlset', 1) // $ExpectType Element
@@ -14,8 +17,6 @@ x(null, 'string') // $ExpectType Root
1417
x(null, 1) // $ExpectType Root
1518
x(null, []) // $ExpectType Root
1619

17-
const xmlns = 'http://www.sitemaps.org/schemas/sitemap/0.9'
18-
1920
x('urlset', {xmlns}) // $ExpectType Element
2021
x('urlset', {xmlns}, 'string') // $ExpectType Element
2122
x('urlset', {xmlns}, ['string', 'string']) // $ExpectType Element
@@ -26,7 +27,6 @@ x('urlset', {xmlns}, x('loc'), x('loc')) // $ExpectType Element
2627
x('urlset', {xmlns}, [x('loc'), x('loc')]) // $ExpectType Element
2728
x('urlset', {xmlns}, []) // $ExpectType Element
2829

29-
const xmlNumberAttribute = 100
3030
x('urlset', {xmlNumberAttribute}, 'string') // $ExpectType Element
3131
x('urlset', {xmlNumberAttribute}, 100) // $ExpectType Element
3232
x('urlset', {xmlNumberAttribute}, x('loc'), 100) // $ExpectType Element

types/test.tsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/** @jsx x */
2+
import x = require('xastscript')
3+
4+
const xmlns = 'http://www.sitemaps.org/schemas/sitemap/0.9'
5+
const xmlNumberAttribute = 100
6+
7+
const t0 = <urlset /> // $ExpectType Element
8+
const t1 = <urlset xmlns={xmlns} /> // $ExpectType Element
9+
const t2 = <urlset xmlNumberAttribute={xmlNumberAttribute} /> // $ExpectType Element
10+
const t3 = <urlset>string</urlset> // $ExpectType Element
11+
// $ExpectType Element
12+
const t4 = (
13+
<urlset>
14+
<text />
15+
</urlset>
16+
)
17+
// $ExpectType Element
18+
const t5 = <urlset>text</urlset>
19+
// $ExpectType Element
20+
const t6 = (
21+
<urlset>
22+
<text />
23+
<text />
24+
</urlset>
25+
)
26+
// $ExpectType Element
27+
const t7 = (
28+
<urlset>
29+
test
30+
<text />
31+
</urlset>
32+
)
33+
// $ExpectType Element
34+
const t8 = (
35+
<urlset>
36+
{['a', 'b'].map((value) => (
37+
<text>{value}</text>
38+
))}
39+
</urlset>
40+
)
41+
42+
const e0 = <urlset xmlns /> // $ExpectError

types/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"compilerOptions": {
33
"module": "commonjs",
44
"lib": ["es2015"],
5+
"jsx": "react",
56
"noImplicitAny": true,
67
"noImplicitThis": true,
78
"strictNullChecks": true,

0 commit comments

Comments
 (0)