5

I am trying to configure Knexfile in TypeScript. I created knexfile.ts with 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;

And then I create knex.ts file to make connection:

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;

But I got an error at (knexConfig[NODE_ENV]), saying that:

(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)

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

What am I doing wrong? Please help.

Swix
  • 1,883
  • 7
  • 33
  • 50
  • `NODE_ENV` needs to be a subtype of `"local" | "development" | "production"` – Aluan Haddad Aug 14 '19 at 19:28
  • @AluanHaddad How do I do that? I'm sorry, I'm still learning TypeScript. This is where I got `NODE_ENV`: ` export const { NODE_ENV = 'development', HOST = '0.0.0.0', PORT = 8081 } = process.env; ` – Swix Aug 15 '19 at 06:33

3 Answers3

8

I believe you can either supress these errors by setting:

  "suppressImplicitAnyIndexErrors": true,

in your tsconfig.json

or you can create an index signature for the knexConfig object in some way:

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

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

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

    production: {
        ...defaults
    }
};

For more possibilities see the possible duplicate of this question: How do I prevent the error "Index signature of object type implicitly has an 'any' type" when compiling typescript with noImplicitAny flag enabled?

madflow
  • 7,718
  • 3
  • 39
  • 54
  • 8
    In case anyone reads this, `Knex` (at least on version `^0.21.17`) has a `Config` typing: `import { Config } from 'knex'` – ionizer Feb 23 '21 at 10:20
5

For the future, at version ^1.0.4, knex export all types in Knex interface, so

import { Knex } from "knex";

and get the config autocomplete

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

I usually do it this way:

import Knex from 'knex'

type DbEnvironments = 'dev' | 'test' | 'stg' | 'prod'

const envs: Record<DbEnvironments, Knex.Config> = {
  dev: {
    client: 'pg',
    connection: {
      database: process.env.DATA_API_DB_NAME,
      host: process.env.DATA_API_DB_SERVICE_HOST,
      port: parseInt(process.env.DATA_API_DB_SERVICE_PORT),
      user: process.env.DATA_API_DB_USER || 'postgres',
      password: process.env.DATA_API_DB_PASSWORD || 'postgres',
    },
    migrations: {
      tableName: 'knex_migrations',
    },
    // debug: true,
  },
  ...
}

export default envs
demisx
  • 7,217
  • 4
  • 45
  • 43