如何在 TypeScript 项目中配置 Knex.ts?

Rar*_*rio 1 postgresql node.js typescript knex.js

我正在尝试在 TypeScript 中配置 Knexfile。我创建knexfile.tsknex init -x ts

const defaults = {
  client: 'postgresql',
  connection: {
    host: DB_HOST,
    user: DB_USER,
    password: DB_PASSWORD,
    database: DB_DATABASE
  },
  pool: {
    min: 2,
    max: 10
  },
  migrations: {
    tableName: 'knex_migrations'
  }
};

const knexConfig = {
  local: {
    client: 'sqlite3',
    connection: {
      filename: './dev.sqlite3'
    }
  },

  development: {
    ...defaults,
    debug: true,
    useNullAsDefault: true
  },

  production: {
    ...defaults
  }
};

export default knexConfig;
Run Code Online (Sandbox Code Playgroud)

然后我创建knex.ts文件来建立连接:

import Knex, { Config } from 'knex';
import knexConfig from '../utils/knexfile';
import { NODE_ENV } from '../utils/config';

// Set environment from `.env`
const knex = Knex(knexConfig[NODE_ENV]);

export default knex;
Run Code Online (Sandbox Code Playgroud)

但是我在 处出错(knexConfig[NODE_ENV]),说:

(alias) const NODE_ENV: string
import NODE_ENV
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ local: { client: string; connection: { filename: string; }; }; development: { debug: boolean; useNullAsDefault: boolean; client: string; connection: { host: string; user: string; password: string; database: string; }; pool: { ...; }; migrations: { ...; }; }; production: { ...; }; }'.
  No index signature with a parameter of type 'string' was found on type '{ local: { client: string; connection: { filename: string; }; }; development: { debug: boolean; useNullAsDefault: boolean; client: string; connection: { host: string; user: string; password: string; database: string; }; pool: { ...; }; migrations: { ...; }; }; production: { ...; }; }'.ts(7053)
Run Code Online (Sandbox Code Playgroud)

================================================== ======

我究竟做错了什么?请帮忙。

mad*_*low 7

我相信您可以通过设置来抑制这些错误:

  "suppressImplicitAnyIndexErrors": true,
Run Code Online (Sandbox Code Playgroud)

在你的 tsconfig.json

或者您可以knexConfig以某种方式为对象创建索引签名:

interface KnexConfig {
    [key: string]: object;
};

const knexConfig: KnexConfig = {
    local: {
        client: 'sqlite3',
        connection: {
        filename: './dev.sqlite3'
        }
    },

    development: {
        ...defaults,
        debug: true,
        useNullAsDefault: true
    },

    production: {
        ...defaults
    }
};
Run Code Online (Sandbox Code Playgroud)

有关更多可能性,请参阅此问题的可能重复项:在启用 noImplicitAny 标志的情况下编译打字稿时,如何防止出现错误“对象类型的索引签名隐式具有‘任何’类型”?

  • 如果有人读到这里,`Knex`(至少在版本`^0.21.17`上)有一个`Config`类型:`import { Config } from 'knex'` (2认同)

小智 6

未来,在版本 ^1.0.4 中,knex 会导出 Knex 接口中的所有类型,因此

import { Knex } from "knex";
Run Code Online (Sandbox Code Playgroud)

并获取配置自动完成

module.exports = {
  client: "mysql",
  connection: {
    filename: path.resolve(__dirname, "src", "database", "connection.ts"),
  },
  migrations: {
    directory: path.resolve(__dirname, "src", "database", "migrations"),
  },
} as Knex.Config;
Run Code Online (Sandbox Code Playgroud)