I've some old code like this
import * as $ from "jquery";
type Foo = string | object | JQuery;
let x: Foo = { bar: 33}; // just a generic object
let y: Foo = { stop: 33}; // meant to a generic object, but...
With Typescript 3.5.1 I get this error for y.
Types of property
stopare incompatible. Typenumberis not assignable to type{ (queue: string, clearQueue?: boolean, jumpToEnd?: boolean): JQuery<HTMLElement>; (clearQueue?: boolean, jumpToEnd?: boolean): JQuery<HTMLElement>; }.
Obviously it's inferring (incorrectly) that {stop:33} is meant to be a JQuery?
Is this a compiler bug? If not, how to avoid the problem?
To explain why Foo is defined that way... It's used as a parameter type to functions, allowing the conversion of Foo to an XML string, like this. Depending on the type of Foo it's treated as an existing XML string, a DOM node, or a generic object which is translated to xml.
private paramToString(data: Foo): string {
let result = isJquery(data) ? new XMLSerializer().serializeToString(data[0]) :
(typeof data === "string" ? data : new XML2JS.Builder().buildObject(data));
return result;
}
Foo? I'm having trouble seeing whatFoois for. I getstring | JQuery, butobject?stopand try. Because may be somestop()functions are there with the same name in inside ofJQueryobject.type Foo = string | object;