0

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
1
  • The object type represents {}, while JSON.parse returns any since it cannot guess what the properties are. Commented Jan 7, 2020 at 7:29

1 Answer 1

1

JSON.parse returns any this is why you don't have an error anymore. The full signature is (see playground here)

JSON.parse(text: string, reviver?: ((this: any, key: string, value: any) => any) | undefined): any

Doing

let parseObj = JSON.parse(JSON.stringify(obj));

is the same as doing

load(obj: any) {
  // ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.