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
In current example it is still possible to workaround this issue.
Consider we importing
Logger
in ourapp.ts
file like so:And to fix it we can import
logger-in-context.ts
file just beforelogger.ts
:But this workaround is only useful in this particular example because we are not doing anything with
LoggerInContext
in thelogger.ts
file at runtime (like we do inlogger-in-context.ts
by using it withextend
keyword) - just returning a new instance only when particular method will be called.