I try to create an instance of concrete class from static method of abstract class. But I have this error:
Uncaught TypeError: Object prototype may only be an Object or null: undefined
On this line of code in ConcreteClass.js: return extendStatics(d, b);
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
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 extendStatics(d, b);
};
My project files:
Program.ts
import { AbstractClass } from "./AbstractClass";
class Program
{
public static Main()
{
let instance = AbstractClass.CreateObject();
instance.Method();
}
}
Program.Main();
AbstractClass.ts
import { ConcreteClass } from "./ConcreteClass";
export abstract class AbstractClass
{
public static CreateObject()
{
return new ConcreteClass();
}
public abstract Method(): void;
}
ConcreteClass.ts
import { AbstractClass } from "./AbstractClass";
export class ConcreteClass extends AbstractClass
{
public Method() : void
{
console.log("Method of ConcreteClass");
}
}
The problem has to do with a circular import. AbstractClass
and ConcreteClass
are importing each other and the definition of each is making use of the other. The specific problem is that when ConcreteClass
is extending AbstractClass
, the AbstractClass
is still undefined, because it is waiting for ConcreteClass
to finish loading. As a result, the runtime is seeing something like vaguely this:
import { AbstractClass } from "./AbstractClass";
// at runtime AbstractClass has not finished loading yet so it is undefined
export class ConcreteClass extends undefined
{
public Method() : void
{
console.log("Method of ConcreteClass");
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments