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 1 commit
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
7 changes: 5 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ type Attributes = Record<string, Primitive>
* @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: string, ...children: Children[]): Element
declare function xastscript(
name: string | null,
...children: Children[]
): Element

/**
* Create XML trees in xast.
Expand All @@ -27,7 +30,7 @@ declare function xastscript(name: string, ...children: Children[]): Element
* @param children (Lists of) child nodes. When strings are encountered, they are mapped to Text nodes.
*/
declare function xastscript(
name: string,
name: string | null,
attributes?: Attributes,
...children: Children[]
): Element
Expand Down
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 Element
x(null, 'string') // $ExpectType Element
x(null, 1) // $ExpectType Element
x(null, []) // $ExpectType Element

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 Element
x(null, {xmlns}, 'string') // $ExpectType Element
x(null, {xmlns}, 1) // $ExpectType Element
x(null, {xmlns}, []) // $ExpectType Element

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