Previously, the code below was functioning properly until typescript 2.0:
class Aluno{
escola: string;
constructor(public nome: string){
this.escola = "";
}
}
class Pessoa{
morada: string;
constructor(public nome: string){
this.morada = "";
}
}
function ePessoa(p: Pessoa | Aluno): p is Pessoa{
return (<Pessoa>p).morada !== undefined;
}
function eAluno(p: Pessoa | Aluno): p is Aluno{
return (<Aluno>p).escola !== undefined;
}
let a: Pessoa | Aluno = new Pessoa("Luis");
console.log( a.nome ); //works fine
a = new Aluno("Rui");
console.log( a.nome ); //works fine as well
//a.escola = "teste";//error occurs here
if(eAluno(a)){
console.log("Student at school:" + a.escola);
}
else{
console.log("Person residing in: " + a.morada); //morada does not exist
}
The compiler is not recognizing 'a' as a Person in the else branch. Am I overlooking something?