gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:26:15 27 4
gpt4 key购买 nike

我正在尝试在 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)

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

我做错了什么?请帮忙。

最佳答案

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

  "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?

关于node.js - 如何在 TypeScript 项目中配置 Knex.ts?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57500089/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com