Skip to content

types: add support for null/fragment type in typescript definition #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 22 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 All @@ -32,4 +40,17 @@ declare function xastscript(
...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 attributes Map of attributes. Nullish (null or undefined) or NaN values are ignored, other values are turned to strings.
* @param children (Lists of) child nodes. When strings are encountered, they are mapped to Text nodes.
*/
declare function xastscript(
name: null,
attributes?: Attributes,
...children: Children[]
): Root

export = xastscript
9 changes: 9 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 @@ -20,6 +25,10 @@ x('urlset', {xmlns}, x('loc')) // $ExpectType Element
x('urlset', {xmlns}, x('loc'), x('loc')) // $ExpectType Element
x('urlset', {xmlns}, [x('loc'), x('loc')]) // $ExpectType Element
x('urlset', {xmlns}, []) // $ExpectType Element
x(null, {xmlns}) // $ExpectType Root
x(null, {xmlns}, 'string') // $ExpectType Root
x(null, {xmlns}, 1) // $ExpectType Root
x(null, {xmlns}, []) // $ExpectType Root

const xmlNumberAttribute = 100
x('urlset', {xmlNumberAttribute}, 'string') // $ExpectType Element
Expand Down