When working in your specific case, there is no need to redeclare the class member...
export class myapp{
myarr = ['me', 'myself', 'i'];
title = this.myarr[0];
detail = this.title ;
}
What is the purpose of TypeScript in this scenario?
TypeScript serves to alert you to the possibility that you have inadvertently declared a duplicate member by preventing such actions.
It is worth mentioning that using the this
prefix for members within a class is not necessary. The resulting JavaScript code will include them...
var myapp = (function () {
function myapp() {
this.myarr = ['me', 'myself', 'i'];
this.title = this.myarr[0];
this.detail = this.title;
}
return myapp;
}());
You can specify an access modifier for the members, as they default to private.
export class myapp{
private myarr = ['me', 'myself', 'i'];
protected title = this.myarr[0];
public detail = this.title ;
}
Access modifiers are only enforced during compile time.