Tag: 部分类

Typescript:从另一个模块的类中添加一个方法/从不同模块填充一个名称空间

故事 我正在build立一个math运算模块库。 我也想把这个库分成多个模块: core , relational , vectors等等。 这些模块可以单独使用(但都取决于core模块) 我知道这是不可能使用部分类如何将TypeScript类分成多个文件? / https://github.com/Microsoft/TypeScript/issues/563 问题 : core模块定义了Set类,它是一个math集。 它定义了诸如Set#add , Set#remove 。 但是, 可选的 relational模块在Set类上添加了Set#product运算符。 其他模块也可以在Set类中添加其他操作。 我想保持添加function的可能性,当我看到适合。 这个问题 使用打字稿 ,我怎样才能添加一个方法在另一个模块居住的类? 我如何安排types,以便我的图书馆用户只有在安装了relational模块的情况下才能在代码完成中看到Set#product ? 否则,他只看到#add和#remove操作? 我正在开发node.js这个库,但也使用browserify捆绑它的浏览器使用。 // core/set.ts export class Set { add(element){} remove(element){} } // relational/set.ts import {Set} from './../core/set.ts'; Set.prototype.product = function(){} // ? // app/index.js import {core} […]

将属性添加到node.js中的嵌套接口

在一个名为“some-file.ts”的文件中,我想这样做: global.someProp = someVar; 而global是在node.d.ts中定义的NodeJS.Globaltypes的预定义对象。 我怎样才能使这个接口部分? 我已经尝试了以下所有无效: 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:“全局”types中不存在属性“someProp” 我该如何解决?