xml-js
xml-js copied to clipboard
Overload type for xml2js
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.
Coming across this issue as well. +1