Provide default value to getter without causing stack overflow in Angular 2 Model Class

So I’m trying to find a way to provide fallback behavior for a getter on a class in JavaScript. The idea is to provide a modified version of another property (title) if the property being accessed is set to null at creation time. The trouble I’m facing is a recursive call when getting the subtitle property because it’s accessing itself. I would simply rename to _subtitle, but with TypeScript you’d have to modify the interface and provide a throwaway temporary value _subtitle in addition to subtitle, which breaks the semantics of the interface.

export interface IFoo {
  title: string;
  subtitle?: string;

export class Foo implements IFoo {
  public title: string;
  public set subtitle(val) {
     this.subtitle = val;
  public get subtitle() {
    return this.subtitle ? this.subtitle : this.title.split('//')[0];

  constructor(obj?: any) {
    this.title = obj && obj.title || null;

Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
newest oldest most voted
Notify of

So I actually ended up realizing shortly after that you can always use something like a private property to serve as the ‘temporary’ storage location without breaking the semantics or contract of the interface. So, with a few simple changes, my setter/getter is working!

  private _subtitle: string;
  public set subtitle(val) {
    this._subtitle = val;
  public get subtitle() {
    return this._subtitle ? this._subtitle : this.title.split('//')[1].trim();