Skip to content
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

Typescript: _comment does not accept string[] #202

Open
threedot14 opened this issue Jun 28, 2023 · 0 comments
Open

Typescript: _comment does not accept string[] #202

threedot14 opened this issue Jun 28, 2023 · 0 comments

Comments

@threedot14
Copy link

Reproducing steps

Consider the following

  const input = {
    _declaration: { _attributes: { version: '1.0', encoding: 'utf-8' } },
    _comment: [
      'Hello1',
      'Hello2'
    ],
    data: {some: 'value'}
  }

  const output= convert.js2xml(xmlData, { compact: true, spaces: 2 })

This code works as a JS module.
This is also explicitly tested in https://github.com/nashwaan/xml-js/blob/f0376f265c4f299100fb4766828ebf066a0edeec/test/test-items.js#L42C30-L42C30

Expected Outcome

This code should also work as a Typescript block as well.

Actual Outcome

TS compiler says _comment does not accept string[]

TS2345: Argument of type '{ _declaration: { _attributes: { version: string; encoding: string; }; }; _comment: string[]; data: { some: string; }; }' is not assignable to parameter of type 'Element | ElementCompact'.
  Type '{ _declaration: { _attributes: { version: string; encoding: string; }; }; _comment: string[]; data: { some: string; }; }' is not assignable to type 'ElementCompact'.
    Types of property '_comment' are incompatible.
      Type 'string[]' is not assignable to type 'string'.

Because,

_comment?: string
defines _comment as string only.

Proposed solution

can we make the type definition this intead?

_comment?: string | string[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant