typescript augment“rx”module =>模块名称无效

时间:2018-02-12 09:34:04

标签: javascript typescript tsc

我正在编写一个也包含类型的节点模块,因为我将它与一个打字稿项目一起使用。该模块取决于"rx": "^4.0.6"。此版本的rx包含rx/ts内部的类型。我想要做的是使用2个自定义函数扩充rx类型。

模块结构

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看起来类似于以下内容(不包括所有提到的函数的实现):

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...

编译模块会导致Invalid module name in augmentation. Module 'rx' resolves to an untyped module at '.../myModule/node_modules/rx/index.js', which cannot be augmented.

我已经阅读了很多关于打字稿中模块扩充的答案,但无法解决我的问题。

修改

/// <reference path="" />中使用rx包含的rx/ts类型解决了Invalid module name问题,但现在我收到了Property 'prototype' does not exist on type 'ObservableStatic'错误

2 个答案:

答案 0 :(得分:0)

您应该安装rx模块的类型:

npm install @types/rx

答案 1 :(得分:0)

通过向Invalid module name中的/// <reference path="" />类型添加rx/ts/rx.all.d.ts来管理以解决index.ts问题。

之后我收到Property 'prototype' does not exist on type 'ObservableStatic'错误,我通过添加以下内容修复了错误:

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

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