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

144
Views
Force a client to call a specific method

How do I force a client to call the build method at the end of the command chain?

const foo = new Foo();
foo.bar().a() // I want to make sure that the `build` method was called.

Here's my example:

interface IFoo {
    bar(): IBar;
    build(): string;
}

class Foo {
    public commands;

    constructor() {
        this.commands = []
    }

    public bar(): any {
        return new Bar(this);
    }

    public build(): any {
        return this.commands.join(' ')
    }

}

interface IBar {
    a(): IFoo
}

class Bar {
    private foo: Foo;

    constructor(foo){
        this.foo = foo;
    }

    public a(): IFoo {
        this.foo.commands.push('something')
        return
    }
}

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

0

Like Peter said, just structure your API such that it provides no utility to a consumer except in the ways you desire:

TS Playground

class Base {
  protected commands: string[];

  constructor (commands?: string[]) {
    this.commands = commands ?? [];
  }

  public build (): string {
    return this.commands.join(' ');
  }

  private toString (): string {
    return this.build();
  }

  private toJSON (): string[] {
    return this.commands;
  }
}

class Foo extends Base {
  public bar (): Bar {
    return new Bar(this.commands);
  }
}

class Bar extends Base {
  public a (): Foo {
    this.commands.push('something');
    return new Foo(this.commands);
  }
}


// Use:

const foo = new Foo();
const bar = foo.bar();
const anotherFoo = foo.bar().a();
const command = anotherFoo.build();

anotherFoo.commands; /*
           ^^^^^^^^
Property 'commands' is protected and only accessible within class 'Base' and its subclasses.(2445) */

console.log(command); // "something"
console.log(JSON.stringify(anotherFoo)); // '["something"]'
console.log(String(anotherFoo)); // "something"
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!