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 someModule = require('someModule')通常被import * as someModule' .但是我不知道如何使用Typescript/ES6语法来表示以下节点。js代码:
var server = require('http').Server(app);
After reading import and call a function with es6 I have tried the following:
在阅读导入并调用es6函数后,我尝试了以下步骤:
import * as httpModule from 'http';
const server = httpModule.Server(app);
and the code does compile and run properly but I still get this TS error:
代码编译并正常运行,但我还是得到了TS错误:
[ts] Property 'Server' does not exist on type 'typeof "http"'
.
属性'Server'不存在于'typeof "http"上。
I have @types/node and @types/express installed. Am I missing something?
我安装了@types/node和@types/express。我遗漏了什么东西?
1 个解决方案
#1
2
Try this:
试试这个:
import { Server, createServer } from 'http';
const server = createServer(app);
这可能会有所帮助。
Clarification: You are using default import instead named import.
说明:您使用的是默认导入,而不是命名导入。
#1
2
Try this:
试试这个:
import { Server, createServer } from 'http';
const server = createServer(app);
这可能会有所帮助。
Clarification: You are using default import instead named import.
说明:您使用的是默认导入,而不是命名导入。