【问题标题】:TypeScript - type annotation for NodeJS moduleTypeScript - NodeJS 模块的类型注解
【发布时间】:2016-01-04 11:49:11
【问题描述】:

在我现在正在处理的 NodeJS 项目中,我们使用依赖注入,以及自定义编写的类的实例,我们曾经将 fscryptohttp 等模块作为参数传递进入构造函数。

现在我们想使用带有 DefinitelyTyped 库的 TypeScript 来支持 NodeJS 类型。对于 NodeJS 模块,我不清楚如何在 TypeScript 中描述模块的类型定义?

例子:

class ReportWriter {
    private fs: ???;
    constructor (fs: ???) { //what should I use instead of ???
        this.fs = fs;
    }
}

我可以创建一个类型别名,例如(或者可能是一个接口):

type NodeFsModule = { 
    readFile: Function, 
    readFileSync: Function 
};

但这需要我描述大量的功能。有没有其他选择?

【问题讨论】:

    标签: typescript definitelytyped


    【解决方案1】:

    只需将importtypeof 一起使用:

    import fs = require('fs');
    class ReportWriter {
        private fs: typeof fs;
        constructor (fs: typeof fs) {
            this.fs = fs;
        }
    }
    

    更多:https://basarat.gitbooks.io/typescript/content/docs/project/declarationspaces.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-05
      • 2017-02-11
      • 1970-01-01
      • 2020-01-14
      • 2016-02-09
      • 2012-10-09
      • 2017-03-06
      相关资源
      最近更新 更多