Issue
Struggling with TypeScript type casting here. Trying to convert an object of type B
to type A
, but without carrying over the properties from type B
.
Inquiry
Is there a way to achieve this conversion without explicitly mentioning the otherName
property? This scenario is simplified compared to my actual problem.
export class A {
id: 0;
name: '';
}
export class B extends A {
otherName: '';
}
Example:
If I try something like this:
const b = new B();
const a1 = <A> b;
const a2 = b as A;
console.log(b, a1, a2);
The output shows:
{id: 0, name: '', otherName: ''}, {id: 0, name: '', otherName: ''}, {id: 0, name: '', otherName: ''}
I'm looking for an outcome like this instead:
{id: 0, name: '', otherName: ''}, {id: 0, name: ''}, {id: 0, name: ''}