Typescript和typeorm config .ts没有重载匹配此调用

时间:2020-07-21 13:55:42

标签: typescript typeorm

你好,我的app.ts有问题 从ormconfig.ts加载我的设置以用于typeorm函数创建连接时 我在我的应用上收到此错误:

No overload matches this call.
  Overload 1 of 3, '(name: string): Promise<Connection>', gave the following error.
    Argument of type 'typeof import("d:/EmasaTi_StockControl/src/shared/infra/http/ormconfig")' is not assignable to parameter of type 'string'.
  Overload 2 of 3, '(options: ConnectionOptions): Promise<Connection>', gave the following error.
    Argument of type 'typeof import("d:/EmasaTi_StockControl/src/shared/infra/http/ormconfig")' is not assignable to parameter of type 'ConnectionOptions'.
      Type 'typeof import("d:/EmasaTi_StockControl/src/shared/infra/http/ormconfig")' is missing the following properties from type 'ExpoConnectionOptions': type, database, driverts(2769)
Peek Problem (Alt+F8)
No quick fixes available

代码:

import 'dotenv/config';
import { createConnection } from 'typeorm';
import App from './app';
import validateEnv from '@utils/validateEnv';
import * as config from './ormconfig';
validateEnv();

(async () => {
  try {
    const connection = await createConnection(config);
    await connection.runMigrations();
  } catch (error) {
    console.log('Error while connecting to the database', error);
    return error;
  }
  const app = new App(
  );
  app.listen();
})();

我的配置文件:

import { ConnectionOptions } from 'typeorm';

const rootDir = process.env.NODE_ENV === 'development' ? 'src' : 'build/src';
export const config: ConnectionOptions = {
  type: 'postgres',
  host: process.env.DB_HOST,
  port: Number(process.env.DB_PORT),
  username: process.env.DB_USER,
  password: process.env.DB_PASS,
  database: process.env.DB_NAME,
  entities: [rootDir + '/entities/**/*.{js,ts}'],
  migrations: [rootDir + '/migrations/*.{js,ts}'],
  subscribers: [rootDir + '/subscribers/**/*.{js,ts}'],
  cli: {
    entitiesDir: `${rootDir}/entities`,
    migrationsDir: `${rootDir}/migration`,
    subscribersDir: `${rootDir}/subscriber`,
  },
  synchronize: false,
  logging: true
};

module.exports = config;

我无法想象如何解决这个问题 如果有人可以帮助我,我将不胜感激

1 个答案:

答案 0 :(得分:1)

我不确定什么是完美的解决方案,但是我找到了解决之道。

  1. 视情况而定,从"type": "mysql"或其他配置文件中删除tsconfig.json
  2. 照常导入您的配置。例如:import config from './ormconfig.json'
  3. 像这样使用config变量:createConnection({type: "postgres", ...config})

我认为出于某些原因,type键没有得到正确的解析。

干杯。