Skip to content

4th overload (name, ...children) is not working as expected #16

Description

@GauBen

Initial checklist

Affected package

xastscript

Steps to reproduce

Hi!

This overload:

* (name: string, ...children: Array<Child>): Element

is not working as expected

import { x } from "xastscript";

const ast = x("foo", x("bar", "baz"));

console.log(JSON.stringify(ast, null, 2));

Actual behavior

{
  "type": "element",
  "name": "foo",
  "attributes": {
    "type": "element",
    "name": "bar",
    "attributes": "[object Object]",
    "children": "[object Object]"
  },
  "children": []
}

Expected behavior

{
  "type": "element",
  "name": "foo",
  "attributes": {},
  "children": [
    {
      "type": "element",
      "name": "bar",
      "attributes": {},
      "children": [
        {
          "type": "text",
          "value": "baz"
        }
      ]
    }
  ]
}

Runtime

No response

Package manager

No response

Operating system

No response

Build and bundle tools

No response

Activity

  1. added
    👋 phase/newPost is being triaged automatically
    🤞 phase/openPost is being triaged manually
    and removed
    👋 phase/newPost is being triaged automatically
    on Oct 10, 2026
  2. JounQin commented on Oct 10, 2026

    @JounQin
    Member

    It's matched as attributes which is expected.

    See also

    xastscript/lib/index.js

    Lines 150 to 161 in c1b6eca

    function isAttributes(value) {
    if (
    value === null ||
    value === undefined ||
    typeof value !== 'object' ||
    Array.isArray(value)
    ) {
    return false
    }
    return true
    }

    So you can try array instead.

    import { x } from "xastscript";
    
    const ast = x("foo", [x("bar", "baz")]);
    
    console.log(JSON.stringify(ast, null, 2));
  3. github-actions commented on Oct 10, 2026

    @github-actions
  4. added
    👎 phase/noPost cannot or will not be acted on
    and removed
    🤞 phase/openPost is being triaged manually
    on Oct 10, 2026
  5. GauBen commented on Oct 10, 2026

    @GauBen
    Author

    @JounQin then the TS type needs to be updated if this is not supported

    From (name: string, ...children: Array<Child>): Element to (name: string, children: Array<Child>): Element maybe?

  6. JounQin commented on Oct 10, 2026

    @JounQin
    Member

    @GauBen No, it's just (name: string, attributes?: Attributes, ...children: Array<Child>): Element been matched.

    x("bar", "baz") matches Attributes, it's hard to distinguish a single Child vs Attributes at runtime.

  7. GauBen commented on Oct 10, 2026

    @GauBen
    Author

    The runtime code matches (name: string, attributes?: Attributes, ...children: Array<Child>): Element but the TS type matches x(name: string, ...children: Array<Child>): Element:

    Image

    ts playground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    👎 phase/noPost cannot or will not be acted on🙋 no/questionThis does not need any changes

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions