xml-js icon indicating copy to clipboard operation
xml-js copied to clipboard

Overload type for xml2js

Open erezwanderman opened this issue 5 years ago • 1 comments

The function xml2js is currently declared as returning "Element | ElementCompact". This means I can't assign it the result to a variable of type Element even if I specify {compact: false}.

const x: Element = convert.xml2js('<a></a>', { compact: false});
// Error	TS2322	(TS) Type 'Element | ElementCompact' is not assignable to type 'Element'.
  Type 'Element' is missing the following properties from type 'Element': assignedSlot, classList, className, clientHeight, and 122 more.

I suggest changing the declaration to something like this:

  xml2js(xml: string): Element;
  xml2js(xml: string, options?: { compact: true } & Options.XML2JS): ElementCompact;
  xml2js(xml: string, options?: { compact: false } & Options.XML2JS): Element;
  xml2js(xml: string, options?: Omit<Options.XML2JS, 'compact'>): Element;
  xml2js(xml: string, options?: Options.XML2JS): Element | ElementCompact;

So that typescript can detect the type when possible.

erezwanderman avatar Nov 19 '19 13:11 erezwanderman

Coming across this issue as well. +1

joshgrift avatar Apr 17 '21 01:04 joshgrift