Cast to parent class instance and only have parent class properties and methods

Viewed 25

I am wondering if there is a way to achieve instance of sample_2 to show only parent classes properties:

class Parent {
    constructor(public name:string) {}
}

class Child extends Parent {
    constructor(public name: string, public age:number) {
        super(name)
    }
}

const sample = new Child("Robin", 30)

console.log(sample)

const sample_2 = sample as Parent // my attempt to type cast

console.log(sample_2)

These two both logs:

[LOG]: Child: {
  "name": "Robin",
  "age": 30
}

What I want to have is:

[LOG]: Parent: {
  "name": "Robin"
}
0 Answers
Related