Skip to content

Commit b41a287

Browse files
authored
Add support for number type in attributes
Closes GH-5. Reviewed-by: Christian Murphy <[email protected]> Reviewed-by: Titus Wormer <[email protected]>
1 parent db94972 commit b41a287

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

types/index.d.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
// TypeScript Version: 3.7
22

3-
import {Attributes, Element, Node} from 'xast'
3+
import {Element, Node} from 'xast'
44

5-
type Children = string | Node | Children[]
5+
type Children = string | Node | number | Children[]
6+
7+
type Primitive = null | undefined | string | number
8+
9+
/**
10+
* Extending Attributes to Support JS Primitive Types
11+
*/
12+
type Attributes = Record<string, Primitive>
613

714
/**
815
* Create XML trees in xast.

types/test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@ x('urlset', {xmlns}) // $ExpectType Element
1515
x('urlset', {xmlns}, 'string') // $ExpectType Element
1616
x('urlset', {xmlns}, ['string', 'string']) // $ExpectType Element
1717
x('urlset', {xmlns}, x('loc'), 'string') // $ExpectType Element
18+
x('urlset', {xmlns}, x('loc'), 100) // $ExpectType Element
1819
x('urlset', {xmlns}, x('loc')) // $ExpectType Element
1920
x('urlset', {xmlns}, x('loc'), x('loc')) // $ExpectType Element
2021
x('urlset', {xmlns}, [x('loc'), x('loc')]) // $ExpectType Element
2122
x('urlset', {xmlns}, []) // $ExpectType Element
2223

24+
const xmlNumberAttribute = 100
25+
x('urlset', {xmlNumberAttribute}, 'string') // $ExpectType Element
26+
x('urlset', {xmlNumberAttribute}, 100) // $ExpectType Element
27+
x('urlset', {xmlNumberAttribute}, x('loc'), 100) // $ExpectType Element
28+
x('urlset', {xmlNumberAttribute}, []) // $ExpectType Element
29+
2330
x() // $ExpectError
2431
x(false) // $ExpectError
2532
x('urlset', x('loc'), {xmlns}) // $ExpectError

0 commit comments

Comments
 (0)