Inspired from C# partial classes for typescript, Simplified π syntax that may help divide functionality of a single class into multiple π¬π¬π¬ class files
Install dependencies with npm:
npm i mustafah/partials
import partial from 'partials';
@partial export class Employee {
@partial work: EmployeeWork;
@partial lunch: EmployeeLunch;
start() {
this.work.doWork();
this.lunch.goToLunch();
}
}
@partial export class EmployeeWork {
@partial model: EmployeeModel;
doWork() {
console.log(`doWork()`);
}
}
@partial export class EmployeeLunch {
@partial model: EmployeeModel;
goToLunch() {
console.log(`goToLunch()`);
}
}
new Employee().start()
// Ouputs:
// goToWork()
// goToLunch()
@partial export class Employee {
@partial work: EmployeeWork;
@partial lunch: EmployeeLunch;
///////////////////////////////////////////////
// Use onInit() instead of class's constructor
/////////////////////////////////////////////
onInit() {
this.work.doWork();
this.lunch.goToLunch();
}
}
If you tried the following scenario, JS Runtime will raise π« Uncaught ReferenceError: Cannot access 'Employee' before initialization error and
@partial export class Employee {
@partial work: EmployeeWork;
name = 'Mustafah';
}
@partial export class EmployeeWork {
@partial employee: Employee;
doWork() {
console.log(`${this.employee.name} doWork()`);
}
}
@partial export class EmployeeWork {
@partial('Employee') employee;
doWork() {
console.log(`${this.employee.name} doWork()`);
}
}
@partial export class EmployeeWork {
@partial(() => Employee) employee;
doWork() {
console.log(`${this.employee.name} doWork()`);
}
}
@partial export class Employee {
@partial work: EmployeeWork;
name = 'Mustafah';
}
export interface IEmployee {
name: string;
}
@partial export class EmployeeWork {
@partial(() => Employee) employee: IEmployee;
}