In an angular project, we have a base class, from which several concrete classes are derived. In few occasions, these classes refer to each other, in a circular way.
When these files are in different classes, the project compiles successfully but fails to run in the browser.
When all classes are taken to a single .ts
file everything works fine, but we have a very long, hard to maintain file.
Is there a way to separate these files? Even an existing pre-compile task that merges the files (in a smart way) will be appreciated.
EDIT: Im' familiar with imports, modules, etc. The problem is circular dependency. Also it can't elegantly be handled by more abstraction. I try to add a minimal reproduction code.
EDIT: Here's a reproduction, creating a warning: It also shows the reason why I'm having the circular dependency to begin with.
base-class.ts:
import {StorageModel} from './storage-model';
import {SubClass1} from './sub-class-1';
export abstract class BaseClass {
children: BaseClass[];
abstract loadFromModel(model: StorageModel);
doSomething() {
if (this.children[0] instanceof SubClass1) {
(this.children[0] as SubClass1).action1();
}
}
}
sub-class-1.ts:
import {BaseClass} from './base-class';
import {StorageModel} from './storage-model';
import {SubClass2} from './sub-class-2';
export class SubClass1 extends BaseClass {
title: string = 'hello';
action1() {
}
loadFromModel(model: StorageModel) {
(this.children[0] as SubClass2).action2();
}
}
and a similar `sub-class-2.ts. I get the following warning in angular cli console:
WARNING in Circular dependency detected:
src/models/sub-class-1.ts -> src/models/base-class.ts -> src/models/sub-class-1.ts
and in browser console:
[WDS] Warnings while compiling.
Circular dependency detected:
src/models/base-class.ts -> src/models/sub-class-1.ts -> src/models/base-class.ts
That said, the code is working.
I could move the load fromModel
method to a visitor class (which I actually have in the project) but then I would not benefit from overloading and locality (the load logic is physically close to all the related code).
However the original code-base creates the following error:
Uncaught TypeError: Object prototype may only be an Object or null: undefined
at setPrototypeOf (<anonymous>)
at __extends (navigation-list-filter-node.ts:7)
__extends
function is not my code:
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype =
b.prototype, new __());
};
})();