问题描述
我知道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 es6I 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""上不存在属性“服务器"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!