【问题标题】:How to configure Knex.ts in TypeScript project?如何在 TypeScript 项目中配置 Knex.ts?
【发布时间】:2019-12-21 07:54:22
【问题描述】:

我正在尝试在 TypeScript 中配置 Knexfile。我用knex init -x ts 创建了knexfile.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;

然后我创建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;

但我在(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)

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

我做错了什么? 请帮忙。

【问题讨论】:

  • NODE_ENV 需要是 "local" | "development" | "production" 的子类型
  • @AluanHaddad 我该怎么做?对不起,我还在学习 TypeScript。这是我得到NODE_ENV的地方:`export const { NODE_ENV = 'development', HOST = '0.0.0.0', PORT = 8081 } = process.env; `

标签: node.js postgresql typescript knex.js


【解决方案1】:

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

  "suppressImplicitAnyIndexErrors": true,

在你的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
    }
};

有关更多可能性,请参阅此问题的可能重复项:How do I prevent the error "Index signature of object type implicitly has an 'any' type" when compiling typescript with noImplicitAny flag enabled?

【讨论】:

  • 如果有人读到这个,Knex(至少在版本^0.21.17)有一个Config输入:import { Config } from 'knex'
猜你喜欢
  • 2019-10-13
  • 1970-01-01
  • 2020-04-05
  • 2021-04-28
  • 2020-10-11
  • 2019-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多