导入 Promise 的正确方法是什么?
What is the correct way to import a Promise?
我正在尝试理解打字稿导入的概念,尤其是导出的 Promise。我按照答案 进行了操作,它起作用了。但是 ts-lint 规则 'no-var-requires' 不喜欢这个解决方案。因此我的问题。
我目前使用的代码(最小示例):
// app.ts
import Express from 'express';
import { createConnection } from 'typeorm';
const app = Express();
const main = async () => {
await createConnection();
}
export const appPromise = main().then(() => app);
// server.ts
import http from 'http';
const appPromise = require('./app').appPromise;
const httpPort = normalizePort(process.env.PORT || '8080');
let httpServer: any;
appPromise.then((app: Express.Application) => {
httpServer = http.createServer(app);
httpServer.listen(httpPort);
});
正如我所说,ts-lint 不喜欢这种导入。所以我尝试将其更改为:
import appPromise = require('./app').appPromise;
但在那种情况下,它不喜欢 .appPromise 部分,然后 (appPromise.then) 在类型 'type of import' 上不存在。我想我不明白关于进口/出口的一些非常重要的事情。
尝试:
import { appPromise } from './app';
我正在尝试理解打字稿导入的概念,尤其是导出的 Promise。我按照答案
我目前使用的代码(最小示例):
// app.ts
import Express from 'express';
import { createConnection } from 'typeorm';
const app = Express();
const main = async () => {
await createConnection();
}
export const appPromise = main().then(() => app);
// server.ts
import http from 'http';
const appPromise = require('./app').appPromise;
const httpPort = normalizePort(process.env.PORT || '8080');
let httpServer: any;
appPromise.then((app: Express.Application) => {
httpServer = http.createServer(app);
httpServer.listen(httpPort);
});
正如我所说,ts-lint 不喜欢这种导入。所以我尝试将其更改为:
import appPromise = require('./app').appPromise;
但在那种情况下,它不喜欢 .appPromise 部分,然后 (appPromise.then) 在类型 'type of import' 上不存在。我想我不明白关于进口/出口的一些非常重要的事情。
尝试:
import { appPromise } from './app';