将库作为 TypeScript 中的参数传递并类型定义内部模块

Passing library as a paramter in TypeScript and type defining internal module

我已经开始使用 TypeScript 编程,但我对如何在某些情况下定义参数类型感到困惑。

这里是:

::::代码::::

// First the references to TypeScript definition files used by the src code
/// <reference path="../lib/express.d.ts"/>;
/// <reference path="../lib/request.d.ts"/>;

// Import modules for the sake of using TypeScript
import express = require('express');
import request = require('request');

var getUuidFromCouchDb = function (httpReq: any, expressRes: express.Response) {
    // Load module/function that parses JSON
    var parseJsonFromRequestWithCheck = require('./parseJsonFromRequestWithCheck');

    // Get a UUID from CouchDB which we can use to create a new document in the database
    httpReq.get('http://127.0.0.1:5984/_uuids', function (httpReqError: any, httpReqRes: any, httpReqBody: any) {
        var resBodyAsJson: JSON = parseJsonFromRequestWithCheck(httpReqBody, expressRes);

        // Do checks on resBodyAsJson
        if (resBodyAsJson && resBodyAsJson.uuids && resBodyAsJson.uuids.length > 0) {
            // UUID in JSON format received from CouchDB - return it to the client
            expressRes.json(resBodyAsJson);
            return;
        } else {
            // Parsing of JSON likely failed in parseJsonFromRequestWithCheck or     malformed data retrieved
            expressRes.sendStatus(500);
            return;
        }
    });
}

module.exports = getUuidFromBackend;

所以问题是: - 我想将 "httpReq" 参数的类型定义为 "type" "request" 库。这可能完全是 - 如果是这样,那么为该参数定义类型的正确方法是什么。使用 "any" 感觉很松散。 - 对于变量“parseJsonFromRequestWIthCheck,我还想为其定义一个类型。它是一个提供 Express JS 响应的模块。在 JSON 中或通过发送状态,如果 JSON 解析失败. 这与 Express JS 响应的 sendStatus 方法有关。正确的方法是什么?

期待您的来信....

非常感谢您....

I would like to define the type of the "httpReq" parameter to be of "type" "request" library

而不是 httpReq: any 你会得到 httpReq: typeof request

For the variable "parseJsonFromRequestWIthCheck I would also like to define a type for that.

而不是 var parseJsonFromRequestWithCheck 你会 import parseJsonFromRequestWithCheck

要了解有关 TypeScript 模块系统中 import 的更多信息:https://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1