【问题标题】:Adding properties to a nested interface in node.js在 node.js 中向嵌套接口添加属性
【发布时间】:2017-02-19 18:10:25
【问题描述】:

在一个名为“some-file.ts”的文件中,我想这样做:

global.someProp  = someVar;

global 是在node.d.ts 中定义的NodeJS.Global 类型的预定义对象。

我怎样才能使这个界面局部化?

我尝试了以下所有方法均无济于事:

interface Global {
        someProp: string;
    }

global.someProp  = someVar;



interface NodeJS.Global {
        someProp: string;
    }

global.someProp  = someVar;



namespace NodeJS{
    interface Global {
        someProp: string;
    }
}

global.someProp  = someVar;

我不断得到:

TS339:“全局”类型上不存在属性“someProp”

我该如何解决?

【问题讨论】:

  • @SteevePitis 不!这是一个完全不同的问题。
  • 然后尝试用不同的方式解释你的目标,因为我从未在 js namespace & interface 中看到那些关键字
  • @SteevePitis 我的问题完全是关于 TypeScript 和使用部分接口,这是 TypeScript 的一个特性。您提到的问题根本与 TypeScript 无关!
  • 好的,在你的标题中说出来 ;)

标签: node.js oop typescript interface partial-classes


【解决方案1】:

我已经解决了。我必须使用关键字“声明”,如下所示:

declare namespace NodeJS{
    interface Global {
        base_dir: string;
    }
}

【讨论】:

    猜你喜欢
    • 2015-01-24
    • 2016-10-11
    • 2013-11-07
    • 2018-12-22
    • 2020-12-05
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 2017-07-22
    相关资源
    最近更新 更多