Typescript at the limits — Recursive Dependencies and Errors

Yoseph
2 min readMar 19, 2021

--

We’re working on a project in Typescript right now that’s kind of interesting. Essentially it’s a psuedo-compiler, used to generate code for an application we’re building. Without getting too much into the details of the application itself, I want to highlight the structure of the code I’m building, and the problem it is creating! I’m probably going to make this and then post a Q on StackOverflow to find a solution, but if you know how to fix the problem, let me know!

The Error

Exception has occurred: TypeError: Object prototype may only be an Object or null: undefinedat setPrototypeOf (<anonymous>)     at extendStatics (c:\......\....js:9:16)     at __extends 

The Scenario

So here’s the scenario. There’s a group of Typescript files. One is the main file, called ts_scope.ts. It has a class called TsScope. It also has a method called addStatement. The idea is addStatement is a generic method used to add ‘statements’ to a private field in the TsScope instance. That class also has a method that when called instantiates a class called TsIf. TsIf will use a reference to addStatement from the TsScope instance to add a statement to the TsScope instance after it does it’s work. If it’s confusing, I’d understand. That’s probably why Node.js is also confused, and raising an exception.

class TsScope {
...
addIf(text: string) {
const tsIf = new TsIf(text, this.addStatement);
class TsIf {
private _text: string;
private _statementerAdder: (tsStatement: TsStatement) => void;
constructor(text: string, statementAdder: (tsStatement: TsStatement) => void) {
this._text = text;
this._statementAdder = statementAdder;
}

The Hypothesis

Now, I’m not sure what is wrong. What I know is that when I move the class TsIf into the same file as TsScope, then i’m fine. Everything works as expected. No error. But if it’s in it’s own file, the error occurs.

The Conclusion

I’ll update this article if I find a solution, but in the meantime, if you have expertise, please let me know what’s happening and the best course of action, assuming I keep the files separate (I want to avoid creating one single massive file with multiple classes in it).

--

--

Yoseph
Yoseph

Written by Yoseph

Software Engineer passionate about the future of cities. Currently building libraries for Azure IoT.

Responses (1)