My data structure looks like this:
export abstract class Person {
...
}
export class FPerson extends Person {
a: A;
b: B;
}
export class JPerson extends Person {
c: C;
}
export class User {
person: Person;
}
When dealing with the same structure written in Java on the server side, the User's person member can either be FPerson
or JPerson
. I need to cast it based on the person type. How can I achieve this in Typescript?
For example:
doSomething(u: User) {
let fp: FPerson;
let jp: JPerson;
if (u.person.isFPerson()) {
fp = <FPerson> u.person;
} else {
jp = <FPerson> u.person;
}
...
}
How do I create the isFPerson()
method? And how can I ensure that TypeScript stores the additional fields from FPerson
and JPerson
into a Person
variable?