Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

348
Vistas
Split TypeScript class in multiple files

Found this article, that explains how to split a class in TypeScript, however, the code seem to be in the same file

// the original class
class Employee {
  doWork: void {
  }
}

// extend it
interface Employee {
  goToLunch(): void;
}
Employee.prototype.goToLunch = function(){
}

could somone give an example on how to extend/split a typescript class in multiple files?

I use Angular in my project.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Move the interface and class into it's own files and add the export modifier for the interface. When using the interface in the class file you will need to import it (your IDE of choice will most likely suggest it already). On the class itself you can then use the implements keyword to use the interface.

This way of using interfaces and separated files will work for Angular and regular Typescript

// Employee.interface.ts
export interface IEmployee {
    goToLunch(): void;
}    

// Employee.model.ts
import { IEmployee } from "./employee.interface";

class Employee implements IEmployee {
    goToLunch(): void {
        // Implement goToLunch logic here
    }
  }
  


If you have a class that you want to extend from the syntax is very similar to using a interface. And can also be done from different files

// WorkEmployee.model.ts
export class WorkEmployee {
    goToWork(): void {
        // Implement goToWork logic here
    }
}

// Employee.model.ts
import { IEmployee } from "./employee.interface";
import { WorkEmployee } from "./work-employee.model";

class Employee extends WorkEmployee implements IEmployee {
goToLunch(): void {
    throw new Error("Method not implemented.");
}
  }

This employee will then have both logic from itself (goToLunch) and the extended class (goToWork)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda