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

201
Views
Implementar interfaces con herencia múltiple en una clase de Nodejs

Intenté la siguiente forma de crear una interfaz e implementarla.

 class AInterface { constructor () { if (!this.methodA) { throw new Error('class should implement a methodA() method') } else if (!this.methodB) { throw new Error('class should implement a methodB() method') } } } export default AInterface

implementó eso en una clase al extenderlo. (Tenga en cuenta que he usado ts-mixer para tener herencia múltiple.

 import AInterface from './AInterface' import { Mixin } from 'ts-mixer' class ClassA extends Mixin(AnotherClass, AInterface) { constructor () { super() } methodA () { return 'test' } methodB () { return 'test' } } export default ClassA

Esto arrojará la class should implement a methodA() method . Lo que significa que la comprobación que hago en la interfaz falla if (!this.methodA) . Esto funciona bien cuando elimino Mixin y extiendo solo la interfaz. ( class ClassA extends AInterface )

¿Hay una mejor manera de hacer esto o cómo puedo solucionarlo?

Versión de nodo - 14

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

El problema parece ser que el constructor de AInterface no obtiene this valor correcto. Entonces no ve el methodA o el methodB

La solución es evitar hacer esa verificación en el constructor.

 import AInterface from "./AInterface.mjs"; import { Mixin, settings } from "ts-mixer"; settings.initFunction = "init"; class ClassA extends Mixin(AnotherClass, AInterface) {}

AInterface.js

 class AInterface { init () { if (!this.methodA) { throw new Error('class should implement a methodA() method') } else if (!this.methodB) { throw new Error('class should implement a methodB() method') } } } export default AInterface

Observe el uso del método init() anterior.

about 4 years ago · Santiago Trujillo 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!