IamSoClueless
IamSoClueless

Reputation: 451

What is the correct way to import a Promise?

I'm trying to understand the concept of typescript imports especially of an exported Promise. I followed the answer https://stackoverflow.com/a/41364294/3969007 and it works. But the ts-lint rule 'no-var-requires' doesn't like this solution. Hence my question.

The code I currently use (min-example):

// 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);
});

As I said, ts-lint doesn't like that import. So I tried to change it to:

import appPromise = require('./app').appPromise;

But in that case, it doesn't like the .appPromise part and later on then (appPromise.then) does not exist on type 'type of import'. I guess I did not understand something very essential regarding imports / exports.

Upvotes: 0

Views: 2565

Answers (1)

Paleo
Paleo

Reputation: 23712

Try:

import { appPromise } from './app';

Upvotes: 1

Related Questions