Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// TypeScript Version: 3.7

import {Element, Node} from 'xast'
import {Element, Node, Root} from 'xast'

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

Expand All @@ -19,6 +19,14 @@ type Attributes = Record<string, Primitive>
*/
declare function xastscript(name: string, ...children: Children[]): Element

/**
* Create XML trees in xast.
*
* @param name Qualified name. Case sensitive and can contain a namespace prefix (such as rdf:RDF).
* @param children (Lists of) child nodes. When strings are encountered, they are mapped to Text nodes.
*/
declare function xastscript(name: null, ...children: Children[]): Root

/**
* Create XML trees in xast.
*
Expand Down
6 changes: 6 additions & 0 deletions types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import x = require('xastscript')

x('urlset') // $ExpectType Element
x('urlset', 'string') // $ExpectType Element
x('urlset', 1) // $ExpectType Element
x('urlset', ['string', 'string']) // $ExpectType Element
x('urlset', x('loc'), 'string') // $ExpectType Element
x('urlset', x('loc')) // $ExpectType Element
x('urlset', x('loc'), x('loc')) // $ExpectType Element
x('urlset', [x('loc'), x('loc')]) // $ExpectType Element
x('urlset', []) // $ExpectType Element
x(null) // $ExpectType Root
x(null, 'string') // $ExpectType Root
x(null, 1) // $ExpectType Root
x(null, []) // $ExpectType Root

const xmlns = 'http://www.sitemaps.org/schemas/sitemap/0.9'

Expand All @@ -30,3 +35,4 @@ x('urlset', {xmlNumberAttribute}, []) // $ExpectType Element
x() // $ExpectError
x(false) // $ExpectError
x('urlset', x('loc'), {xmlns}) // $ExpectError
x(null, {xmlns}) // $ExpectError