Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Trying to use NestJS and TypeORM? Trying to use the provided ConfigModule with forRootAsync? Cool, you might get an error.

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        type: 'postgres',
        host: configService.get('DB_HOST'),
        port: configService.get('DB_PORT'),
        username: configService.get('DB_USERNAME'),
        password: configService.get('DB_PASSWORD'),
        database: configService.get('DB_NAME'),
        synchronize: false,
        autoLoadEntities: true,
      }),
      inject: [ConfigService],
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

In this case, the error is like so:

Type '(configService: ConfigService) => Promise<{ type: string; host: any; port: any; username: any; password: any; database: any; synchronize: boolean; autoLoadEntities: boolean; }>' is not assignable to type '(...args: any[]) => ({ retryAttempts?: number; retryDelay?: number; autoLoadEntities?: boolean; keepConnectionAlive?: boolean; } & Partial<MysqlConnectionOptions>) | ... 13 more ... | Promise<...>'.

Not very helpful, but a solution exists. But it's not great.

Cast the postgres indicator to like so:

type: 'postgres' as 'postgres'

Hopefully this gets resolved soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.