Through the interface it will not work, but you can do this:
interface IUserData{
name: string;
gender: string;
secret: string;
}
class PublicUserData {
name: string;
gender: string;
constructor(data: IUserData){
this.name = data.name;
this.gender = data.gender;
}
}
class UserData extends PublicUserData implements IUserData {
secret: string;
constructor(data: IUserData){
super(data);
this.secret = data.secret;
}
}
And so use:
var data = {
name: 'Jon',
gender: 'male',
secret: 'xxx'
} as IUserData;
var publicUserData = new PublicUserData(data);
var userData = new UserData(data);
var publicUserData2 = new PublicUserData(userData);
Result:
publicUserData: {name: "Jon", gender: "male"}
publicUserData2:{name: "Jon", gender: "male"}
userData: {name: "Jon", gender: "male", secret: "xxx"}
Of course to use the boxing/unboxing mechanism through casting types looks better, but this can not hide properties. Therefore, it is necessary to give types explicitly.