Node.js 为噩梦扩展键入

Node.js 为噩梦扩展键入,node.js,typescript,typescript-typings,nightmare,Node.js,Typescript,Typescript Typings,Nightmare,我用的是《噩梦》课的打字。这是一个通过npm install@types/dream进行的安装 我想在不修改node_模块的index.d.ts的情况下扩展现有的打字。特别是通过添加action和evaluate_now方法。 动作是一种静态方法 这就是我所做的 我在我的项目根文件夹中创建了一个自定义打字文件 定制打字 在我的主应用程序文件中,我有以下内容 索引 我得到以下错误 类型“噩梦”上不存在属性“立即求值”。 类型“typeof噩梦”上不存在属性“action”。 我用的是打字脚本3。似

我用的是《噩梦》课的打字。这是一个通过npm install@types/dream进行的安装

我想在不修改node_模块的index.d.ts的情况下扩展现有的打字。特别是通过添加action和evaluate_now方法。 动作是一种静态方法

这就是我所做的 我在我的项目根文件夹中创建了一个自定义打字文件

定制打字

在我的主应用程序文件中,我有以下内容

索引

我得到以下错误

类型“噩梦”上不存在属性“立即求值”。 类型“typeof噩梦”上不存在属性“action”。 我用的是打字脚本3。似乎没有检测到我的自定义打字。我一直在抱怨,但我不知道我做错了什么


谢谢

您在custom-typings.d.ts中声明的全局名称空间与模块无关。相反,您需要扩充模块:

declare module "dummy" {
  module "nightmare" {
    // ...
  }
}

但是,梦魇类是在原始typings export=monamine中指定的导出类,而AFAIK export指定的类目前无法扩充;看见因此,您必须在项目中添加@types/dream的修改副本。

谢谢Matt。这不是关于默认导出类的问题,而不是使用export=导出的类的问题吗?对。我刚刚对这个问题添加了一条评论,指出我相信导出指定类也有同样的问题。我认为再提出一个问题是没有帮助的。
/// <reference path='custom-typings.d.ts'/>

import Nightmare = require('nightmare');

function size(this: Nightmare, done: any) {
  this.evaluate_now(() => {
    const w = Math.max(
      document.documentElement.clientWidth,
      window.innerWidth || 0
    );
    const h = Math.max(
      document.documentElement.clientHeight,
      window.innerHeight || 0
    );
    return {
      height: h,
      width: w
    };
  }, done);
}

Nightmare.action('size', size);

// no errors here from the types declared by the @types in node_modules.
new Nightmare()
  .goto('http://yahoo.com')
  .type('input[title="Search"]', 'github nightmare')
  .click('.searchsubmit');
declare module "dummy" {
  module "nightmare" {
    // ...
  }
}