Home > Software engineering >  "Typescript" return "Object" intead my class
"Typescript" return "Object" intead my class

Time:10-23

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".

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".

CodePudding user response:

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 { ... }
  • Related