I am looking for a way to detect if an object literal is in fact a "non-instantiated" object of a certain class. I hope it will be more clear with an example:
class Range{
from: number;
to: number;
constructor(from: number | Range, to?: number){
if (from instanceof Range)
Object.assign(this, from)
else{
this.from = from;
this.to = to;
}
}
average(){return (this.to -this.from) / 2;}
}
let one = new Range(1, 20)
let copyOfOne = structuredClone(one); // This generates an object literal (or kind of ?), the method "average" is not in the prototype
let two = new Range(copyOfOne) // Constructor won't detect the parmeter as instanceof Range
Is there a way to know if an object literal is in fact a "non-instantiated instance" of a certain class? (of course I could check if the object has the class properties, here is simple but with more complex objects would be impractical)