2

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"
}
suchcodemuchwow
  • 848
  • 2
  • 9
  • 27
  • 1
    No, there is no runtime type casting in TypeScript. If you want to remove properties you will need to do so at runtime, explicitly. – jcalz Jul 27 '22 at 01:43

0 Answers0