Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

215
Views
TypeScript immutable data/classes with mutual reference

Usually when I create a TypeScript application I follow an OOP + immutable data philosophy.

So let's suppose I have two classes with mutual reference, class A and class B.

class A {
    public readonly item: B;

    public constructor (item: B) {
        this.item = item;
    }
}

class B {
    public readonly item: A;

    public constructor (item: A) {
        this.item = item;
    }
}

Now it's impossible to create instance of class A, since class B is required in constructor to achieve immutability, and class B requires instance of class A.

It's required to create first an instance of class A and B with item property set to undefined and then respectively assign the references after class instantiation. But this solution will break immutability since it will allow users to overwrite the item property.

Any proposed solution?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

One way is use getter to achieve lazy evaluation,

class A {
  private readonly _item: () => B;
  public get item(): B {
    return this._item();
  }

  public constructor(_item: () => B) {
    this._item = _item;
  }
}

class B {
  public readonly _item: () => A;
  public get item(): A {
    return this._item();
  }
  public constructor(_item: () => A) {
    this._item = _item;
  }
}

const a: A = new A(() => b);
const b: B = new B(() => a);

console.log(a.item === b); // true
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!