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
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;
}
}