Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"Typescript" return "Object" intead my class

I have a project with react and typescript.

I have created a model to parse data coming from an endPoint.

export class MyClassModel{
    constructor(
        public name:string,
        public url:string,
    ) {}

    get names():string{
        return this.name
    }

    static parsedResponse = ({name,url}:Result):MyClassModel=> {

        const model =  new MyClassModel(
            name,
            url
        )

        return model;
    }

}

My model class has a static method that returns a new object of my class, but when I view the response through the console the method returns an object of type "OBJECT" instead of an object of type "MyClass".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

console.log(new MyClassModel("name","https://www.example.com/"))
result:

Result in console

Is this normal? I have worked with Vue3, using the same logic and normally the console shows the type "MyClass".

>Solution :

That’s browser specific. For instance, if you do:

class X {}
new X();

the Chrome devtools show

▼ X {}
  ▼ [[Prototype]]: Object 
    ► constructor: class X
    ► [[Prototype]]: Object

while the firefox devtools show

▼ Object {  }
  ▼ <prototype>: Object { ... } 
    ► constructor: class X {}
    ► <prototype>: Object { ... }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading