-1

I am writing a node module that also contains types, since I'm using it with a typescript project. The module depends on "rx": "^4.0.6". This version of rx contains the types internally in rx/ts. What I want to do is augment the rx types with 2 custom functions.

Module structure

myModule
|-- ts
    |-- index.ts
|-- package.json
|-- tsconfig.json

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "declaration": true,
    "rootDir": "ts",
    "outDir": "./dist"
  }
}

package.json

{
  "name": "myModule",
  "version": "1.2.0",
  "private": true,
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "scripts": {
    "preinstall": "npm run build",
    "build": "../../node_modules/typescript/bin/tsc",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "@types/express": "^4.0.35",
    "express": "^4.0.35",
    "rx": "^4.0.6"
  },
  "devDependencies": {
    "typescript": "~2.3.4"
  }
}

ts/index.ts looks something like the following (didn't include the implementation of all mentioned functions):

import * as Rx from 'rx';
import * as express from 'express';

declare module "rx" {
    interface Observable<T> {
        pipeValueExpressResponse: (res: express.Response) => void;
        pipeArrayExpressResponse: (res: express.Response) => void;
    }
}

// export some types...

Rx.Observable.prototype.pipeValueExpressResponse = function(res: 
express.Response) {
    sendValue(this, res);
};

Rx.Observable.prototype.pipeArrayExpressResponse = function(res: 
express.Response) {
    foldResponse(this, res);
};

// export some functions...

Compiling the module leads to Invalid module name in augmentation. Module 'rx' resolves to an untyped module at '.../myModule/node_modules/rx/index.js', which cannot be augmented.

I have read a lot answers regarding module augmentation in typescript but wasn't able to fix my problem.

EDIT

Using a /// <reference path="" /> to the included rx types in rx/ts solved the Invalid module name issue but now I get a Property 'prototype' does not exist on type 'ObservableStatic' error

XeniaSis
  • 2,192
  • 5
  • 24
  • 39

2 Answers2

0

You should install the types for the rx module :

npm install @types/rx
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357
  • I get `Duplicate identifier` errors doing so. And since types already exist in the `rx` module, I don't see why I should do that. I guess this would be my last solution, but even in this case, I still get a lot of errors. – XeniaSis Feb 12 '18 at 09:40
  • @XeniaSis you should figure out where the duplicate types come from, maybe there are other definitions you have installed, I installed just `@types/rx` and I can augment the module fine. Try running the install in an empty folder and compile the code there and see that it works – Titian Cernicova-Dragomir Feb 12 '18 at 09:42
  • Isn't there a way to just "import" the already included types? It also seems that the `@types/rx` are not complete/compatible with the version I use – XeniaSis Feb 12 '18 at 09:50
0

Managed to fix the Invalid module name issue by adding a /// <reference path="" /> to the rx/ts/rx.all.d.ts types in index.ts.

After that I got a Property 'prototype' does not exist on type 'ObservableStatic' error which I fixed by adding the following:

declare module "rx" {
    interface ObservableStatic {
        prototype: any;
    }

    interface Observable<T> {
        pipeValueExpressResponse: (res: express.Response) => void;
        pipeArrayExpressResponse: (res: express.Response) => void;
    }
}
XeniaSis
  • 2,192
  • 5
  • 24
  • 39