【发布时间】: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