6
class base {
  a: number;
  b: number;
}

class child extends base {
  c: number;
}

let obj: child = { a: 4, b: 5, c: 6 };
let test: base = obj as base;
console.log(test);

Will output { a: 4, b: 5, c: 6 }

How to output { a: 4, b: 5 } ?

1 Answer 1

4

Types do not change how your program works. They only exist in during "compilation".

To change the output you will have to make your code remove the property.

See

So, to output only a and b you have to call a code to delete c

delete obj.c;
1
  • And I see as well that the property definition also disappear after compilation, so no way to use hasOwnProperty... I would like to find a generic way if the class properties change. With delete, I will have to change the code to different place. It would be great to be able to change only the structure of the class. :-/
    – Alexandre
    Jan 8, 2017 at 1:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.