我正在尝试在TypeScript中配置Knexfile。我使用knexfile.ts
创建了knex 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;
然后我创建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)
================================================ =========
我在做什么错? 请帮忙。
答案 0 :(得分:2)
我相信您可以通过设置以下错误来抑制这些错误:
"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?