Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createApp returns any type #1299

Open
HRK44 opened this issue Jan 29, 2025 · 3 comments
Open

createApp returns any type #1299

HRK44 opened this issue Jan 29, 2025 · 3 comments
Labels

Comments

@HRK44
Copy link

HRK44 commented Jan 29, 2025

Hello,

As the title says, the createApp function returns type any which creates bunch of problems

Would it be possible to type it? if not, what's the best approach on a simple index.ts file:

import 'source-map-support/register';

// 3p
import { Config, createApp, getHttpLogParamsDefault, Logger, ServiceManager } from '@foal/core';

// App
import { AppController } from './app/app.controller';
import { dataSource } from './db';
import { getTraceInfo } from '@myapp/server-logger';

async function main() {
    await dataSource.initialize();

    const serviceManager = new ServiceManager();
    const logger = serviceManager.get(Logger);

    const app = await createApp(AppController, {
        serviceManager,
        getHttpLogParams: (tokens, req, res) => {
            const traceInfo = getTraceInfo();
            return { ...getHttpLogParamsDefault(tokens, req, res), ...traceInfo };
        }
    });

    const port = Config.get('port', 'number', 3001);
    app.listen(port, () => logger.info(`Listening on port ${port}...`));
}

main().catch(err => {
    console.error(err.stack);
    process.exit(1);
});
@HRK44 HRK44 added the question label Jan 29, 2025
@LoicPoullain
Copy link
Member

As the title says, the createApp function returns type any which creates bunch of problems

In the past, the value returned by createApp was typed using @types/express. This unfortunately caused a lot of problems, as this type package is really unstable between two versions. This led to the application not compiling because some Express middleware used one specific version of @types/express while Foal used another. This is why it was originally removed.

if not, what's the best approach on a simple index.ts file:

If you still want to type it, I think the easiest war is probably to install manually @types/express and use the proper type with as MyType when using createApp.

@HRK44
Copy link
Author

HRK44 commented Jan 30, 2025

Thanks for the quick response, what about using a generic type like createApp<Express>(AppController, options); ?

async function createApp<T = any>(
  controller: any, 
  options?: any
): Promise<T> {

@LoicPoullain
Copy link
Member

Why not, do you want to submit a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants