Access inherited functions from an abstract class inside a TypeScript module

  angular, node.js, typescript

I have an abstract class in TypeScript that I am inheriting for every feature module in my Angular app. This has worked as long as that class was part of the project. Here is a shortened equivalent to the class (it’s called Patchbay).

export abstract class Patchbay<A extends AuthSocket> {
    protected readonly sockets = {};

    constructor() {
        this.sockets = {};
    }

    protected addSocket = (name: string, socket: Socket) {
        this.sockets[name] = socket;
    }

    auth = (socket?: A) A => {
          // do random stuff
    }
}

Now I’ve decided to put it into a node module of its own to be able to use it on several projects. The trouble is that now I can only access the functions (addSocket, auth) from withhin the subclass that inherits from the abstract class Patchbay, if I use ‘super.auth()’, but not if I use ‘this.auth()’. And I cannot access them at all from outside the class.

Here is the tsconfig of the Patchbay module:

{
    "compilerOptions": {
        "declaration": true,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "lib": [
             "es2015",
             "dom"
        ],
        "module": "commonjs",
        "moduleResolution": "node",
        "noImplicitAny": false,
        "outDir": "./dist/",
        "strict": true,
        "sourceMap": true,
        "target": "es5"
   },
   "include": ["./src"],
   "exclude": ["node_modules", "**/_tests/**"] 
}

And here is the resulting patchbay.d.ts (again stripped down to the essentials):

export declare abstract class Patchbay<A extends AuthSocket> {
    protected readonly sockets: {
        [key: string]: Socket;
    };
    protected constructor();
    protected addSocket: (name: string, socket: Socket) => void;
    auth: (socket?: A | undefined) => A;
}

Now, I kind of assume that I need to declare the module and the class properly for the consuming project to be able to view type Patchbay properly. But where do I do that? The patchbay.d.ts is generated automatically, so I cannot put anything in there. Unfortunately, all tutorials I could find only create extremely minimal modules as examples and so they don’t cover that. Also, the TypeScript documentation itself is rather vague on the matter of where to declare namespaces, modules, etc.

Any help or finger pointing to the right direction is highly appreciated.

Source: Angular Questions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.