node.js - 引用 Typescript .d.ts 文件中定义的其他库类型/接口(interface)

标签 node.js typescript sequelize.js

编辑以尝试更具体(删除旧示例):

我想知道如何在我自己的类和接口(interface)中引用 .d.ts 文件中定义的类型或接口(interface)作为函数参数或类属性的类型。

这是一个例子。我将解释一些不完全的细节
与问题相关,但解释了代码示例:

var fs = require("fs");
var path = require("path");
var Sequelize = require("sequelize");
var config = require('../config/environment');

class SequelizeDB{
    sequelizeInstance: any;
    sequelize: any;
    modelDirectory: string;
    constructor(Sequelize, dbConfig){  
        //instantiate the sequelize instance object
        this.sequelize  = Sequelize;
        this.sequelizeInstance = this.getDatabaseInstance(this.sequelize,
                                                        dbConfig.databaseName, 
                                                        dbConfig.userName, 
                                                        dbConfig.password, 
                                                        dbConfig.hostName,
                                                        dbConfig.port,
                                                        dbConfig.dialect);
       this.modelDirectory = __dirname + '/sequelize';                                                                                                                                                                                                         
       this.importDataModels();
       this.associateDataModels();
    }
    private getDatabaseInstance(Sequelize,
                        databaseName: string, 
                        userName: string, 
                        password: string, 
                        hostName: string, 
                        port: number, 
                        dialect: string){

        return new Sequelize(
            databaseName, 
            userName,
            password, 
            //options
            {
                host: hostName,
                dialect: dialect,
                port: port,
                pool: {
                    max: 5,
                    min: 0,
                    idle: 10000
            },
        });    
    }
    private importDataModels(){
        //read in all the models from the current folder 
        fs
        .readdirSync(this.modelDirectory)
            .filter((fileName) => {
            return  this.isModelFile(fileName);
        })
        .forEach((file) =>{
            //create the models in the seuelizeInstance
            var model = this.sequelizeInstance.import(path.join(this.modelDirectory, file));
            this[model.name] = model;
        });
    }
    private isModelFile(fileName: string){
        return (fileName.indexOf('.') !== 0) && 
               (fileName !== 'index.js') &&  
               (fileName.indexOf('.map') === -1) &&  //ignore map files
               (fileName.indexOf('.ts') === -1); //ignore ts files
    }
    private associateDataModels(){
        Object.keys(this).forEach((modelName) => {
            if (typeof( this[modelName]) === 'object' &&  'associate' in this[modelName]) {
                this[modelName].associate(this);
            }
       });
    }

};

module.exports = new SequelizeDB(Sequelize, config.sql);

我正在使用 Sequelize,并且我设置了一个模式
根据他们的文档使模型的创建变得容易。这
当前设置对我有用,代码正在做的是创建一个类
创建时设置数据库连接。

然后循环遍历模型文件夹中的所有文件并创建所有模型
使用 Seuquelize.import。

现在我想知道的是如何更换这部分以使用实际的
.d.ts 文件中用于 Sequelize 的类型(此处)[https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/sequelize/sequelize.d.ts] .

在我看来,有一个 Sequelize 静态对象用于创建(小写)sequelize 实例。像这样:
new Sequelize(//config values)

我想将这两件事存储在我在上面的代码中创建的类中,目前为了让它们通过编译器,我将它们设为 any .
sequelizeInstance: any;
sequelize: any;

我将如何能够利用存在的 Sequelize .d.ts 文件并更明确地了解我期望在该对象中的类型以及传递给其构造函数的类型。

这不是唯一的例子,但我更普遍地问的是我可以在 TypeScript 中创建一个接口(interface)或一个类
并强制一个函数采用该特定类或接口(interface),例如:
interface IUser{
    id: number,
    username: string,
    passwork: string    
}

function validateUser(user: IUser): boolean{
    //implementation
}

如果用户界面是在我使用 npm 安装并使用 tsd 安装的 Node 中的模块中定义的,我该怎么做?

如果这还不够详细,请告诉我,我会尽力解释更多。

最佳答案

最简单的方法是使用

/// <reference path="../typings/sequelize/sequelize.d.ts" />

从那时起,您将能够使用该 d.ts 文件中的接口(interface)。编译器也会知道在编译中包含这个文件,所以在缺少的接口(interface)上没有错误。

您拥有的最高级选项是使用 tsconfig.json (大多数 IDE 都理解这个文件),并在 中指明您项目的所有文件(包括来自 tsd 的 .d.ts)。 tsconfig.json 并且编译器将“知道”编译中要包含的内容。

最好的办法实际上是像这样导入文件:
import * from '../typings/sequelize/sequelize'; // without extension

并且编译器将在编译您的代码之前获取该文件。如果它只有环境声明(d.ts),它不会将它包含在最终结果中(这很好)。

关于node.js - 引用 Typescript .d.ts 文件中定义的其他库类型/接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33708247/

相关文章:

angular - 在运行时定义 BlockUi Angular 注释?

javascript - Node 和 Sequelize 新手,并获得 "Unhandled rejection TypeError: Vacation.create is not a function"

javascript - 最大限度地减少通过网络发送的数据 (socket.io)

node.js - Sequelize 如何删除关联表中的事件

angular - 如何以 Angular react 形式检查所有复选框

javascript - 使用 belongsTo 对链查找进行后续处理

node.js - 如何在sequelize.js字段validation.isIn.args中编写正则表达式

javascript - 如何在koa中使用 "http"模块?

mod-rewrite - node.js 的 apache mod_rewrite 等效项?

javascript - 未使用的属性(property)下降?