类型"typeof"http""上不存在属性“服务器" [英] Property 'Server' does not exist on type 'typeof "http"'

查看:282
本文介绍了类型"typeof"http""上不存在属性“服务器"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道var someModule = require('someModule')通常由import * as someModule from 'someModule'代替,但是我不知道如何使用Typescript/ES6语法来表示以下Node.js代码:

I know that var someModule = require('someModule') is generally replaced by import * as someModule from 'someModule' but I can't figure out how to use Typescript/ES6 syntax to express the following Node.js code:

var server = require('http').Server(app);

阅读导入并使用es6调用函数 我尝试了以下方法:

After reading import and call a function with es6 I have tried the following:

import * as httpModule from 'http';
const server = httpModule.Server(app);

并且代码确实可以编译并正常运行,但是我仍然遇到此TS错误:

and the code does compile and run properly but I still get this TS error:

[ts] Property 'Server' does not exist on type 'typeof "http"'.

我安装了@ types/node和@ types/express.我想念什么吗?

I have @types/node and @types/express installed. Am I missing something?

推荐答案

尝试一下:

import { Server, createServer } from 'http';
const server = createServer(app);

这可能有帮助.

说明:您使用的是默认导入,而不是名为import .

这篇关于类型"typeof"http""上不存在属性“服务器"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆