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

346
Views
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 answers
Answer question

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 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!