So consider we have Logger class. And also we have a LoggerInContext class which extends Logger. And Logger has factory method to create new LoggerInContext instances.
// logger.ts
import { LoggerInContext } from './logger-in-context';
export class Logger {
// Some methods like log()...
getInContext(context) {
return new LoggerInContext(context);
}
}
// logger-in-context.ts
import { Logger } from './logger';
export class LoggerInContext extends Logger {
constructor(public context) {
super();
}
// Here we can override some behavior
}
So as we will try to import logger.ts
into our application Webpack will then run into file logger-in-context.ts
but at that time Logger
will be undefined so our extend will fail and stack trace will look something like this
(TS compiler is doing his job here as well):
Uncaught TypeError: Cannot read property 'prototype' of undefined
at __extendsFn (index.js:15)
at eval (logger-in-context.ts:4)
at eval (logger-in-context.ts:50)
at Object../src/app/services/logger/logger-in-context.ts (index.js:718)
at __webpack_require__ (index.js:48)
at eval (logger.service.ts:5)
at Object../src/app/services/logger/logger.service.ts (index.js:726)
at __webpack_require__ (index.js:48)
at eval (index.ts:5)
at Object../src/app/services/logger/index.ts (index.js:702)
at __webpack_require__ (index.js:48)
at eval (auth-interceptor.service.ts:7)
at Object../src/app/services/sso/auth-interceptor/auth-interceptor.service.ts (index.js:742)
at __webpack_require__ (index.js:48)
at eval (index.ts:5)
And in generated code error comes from here (simplified a bit):
var logger_1 = __webpack_require__("./src/logger.ts");
// ...
__extends(LoggerInContext, logger_1.Logger); // <--- This is yet undefined
Here's a more generic fix that works in general for any almost all circular dependencies (maybe all of them?)
For your simple case, @gund's solution would work. For the general case, and even with complicated dependencies, this new solution should always work with cases where the needed dependency is required at module evaluation time while the other file does not require a dependency at module evaluation time. Something to consider: if both files depend on each other at module evaluation time, then you'll need a more complicated solution, and possibly it merits a code redesign.