gpt4 book ai didi

javascript - 如何在 TypeScript 中使用 Node `http` 模块

转载 作者:数据小太阳 更新时间:2023-10-29 04:01:23 26 4
gpt4 key购买 nike

我需要用 TypeScript 和 Node 编写一个服务器。

  1. 我下载了Node来自 DefinitelyTyped存储库
  2. 我创建了我的 typescript 文件
  3. 导入定义
  4. 尝试使用它

结果是:

/// <reference path="definitions/commonjs.d.ts" />
/// <reference path="definitions/node.d.ts" />

var http = require("http");

namespace MyProj {
export class Server {
public run() {
var server = http.createServer(); // TypeScript does not recognize 'http'
}
}
}

但我无法理解如何引用 http 模块。我在哪里可以找到类型?在定义文件中,我很难识别此信息。

最佳答案

这是因为你使用了require。使用 import 代替它会识别并且会给你很好的智能感知:-)

import * as http from "http"

关于javascript - 如何在 TypeScript 中使用 Node `http` 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38941636/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com