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

130
Views
Obligar a un cliente a llamar a un método específico

¿Cómo obligo a un cliente a llamar al método de build al final de la cadena de comandos?

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

Aquí está mi ejemplo:

 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

Como dijo Peter , solo estructure su API de tal manera que no proporcione ninguna utilidad a un consumidor, excepto en las formas que desee:

Parque infantil TS

 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!