I'm somewhat perplexed by the behavior of the Typescript compiler with this code. I'm using JSON.parse to populate a class instance from another class instance's stringify output.
From what I can tell the return value of JSON.parse is a regular object. When I pass the object to a load method, while it works, the compiler complains that it can't find the property. However if the object is reparsed (i.e. JSON.parse(JSON.stringify(obj)) within the load method, no more complaints. What is different between the passed object parameter and the object parsed within the method that makes one "unknwon" the compiler, and the other known? Is there actually any difference between these two, or it some kind of type checking glitch?
class Address {
street = "12 Main Street"
city = "Fooville"
load(obj: object) {
this.hello();
this.street = obj.street; // <-- "Property street does not exist on type object
this.city = obj.city; // <-- "Property city doe snot exist on type object
this.hello();
let parseObj = JSON.parse(JSON.stringify(obj));
this.street = parseObj.street; // <-- no complaints
this.city = parseObj.city; // <-- no complaints
console.log(parseObj.constructor.name);
console.log(obj.constructor.name);
}
hello() {
console.log(`My address is ${this.street}, ${this.city}`);
}
}
let foo = new Address();
foo.city="Raleigh"
let jstring = JSON.stringify(foo);
let goo = new Address();
let jparse = JSON.parse(jstring);
goo.load(jparse);
goo.city = "Denver"
goo.hello();
foo.hello();
console out:
My address is 12 Main Street, Fooville
My address is 12 Main Street, Raleigh
Object
Object
My address is 12 Main Street, Denver
My address is 12 Main Street, Raleigh
objecttype represents{}, whileJSON.parsereturnsanysince it cannot guess what the properties are.