knex: Unable to use most Knex types with my TypeScript code due to their not being exported

Environment

Knex version: 0.95.0

Updating from: 0.21.12, where the code shown later did work.

Bug

  1. Explain what kind of behaviour you are getting and how you think it should do

The types in typings should be exported so they can be used in applying types to code using Knex.

  1. Error message

A config builder function declared like so:

const knexConfig = (
  {
    RDS_DB_NAME,
    RDS_USERNAME,
    RDS_PASSWORD,
    RDS_HOSTNAME,
    RDS_PORT,
    source,
  } = configFromEnv()
): Knex.Config<Knex.MsSqlConnectionConfig> => ({
// …config options here…
})

Fails to compile with tsc due to Knex.Config and Knex.MsSqlConnectionConfig not being exported:

TSError: knexfile.ts:50:9 - error TS2694: Namespace '"/foo/node_modules/knex/types/index".Knex' has no exported member 'Config'.

50 ): Knex.Config<Knex.MsSqlConnectionConfig> => ({
           ~~~~~~
knexfile.ts:50:21 - error TS2694: Namespace '"/foo/node_modules/knex/types/index".Knex' has no exported member 'MsSqlConnectionConfig'.

50 ): Knex.Config<Knex.MsSqlConnectionConfig> => ({
                       ~~~~~~~~~~~~~~~~~~~~~

I attempted a workaround, but then run into issue reassuring the knex function that the argument is the expected type, since I can’t as Knex.Config either:

TSError: knexfile.ts:91:40 - error TS2694: Namespace '"foo/node_modules/knex/types/index".Knex' has no exported member 'Config'.

91   const knex = knexCtor(config as Knex.Config)
                                          ~~~~~~
  1. Reduced test code, for example in https://npm.runkit.com/knex or if it needs real database connection to MySQL or PostgreSQL, then single file example which initializes needed data and demonstrates the problem.

This should do it. I couldn’t find an online runner for TypeScript that’d let me import a module, but drop this in a TS project:

import knex, { Knex } from 'knex'

const config: Knex.Config = {}

It will error with something like:

TSError: knexfile.ts:9:20 - error TS2694: Namespace '"foo/node_modules/knex/types/index".Knex' has no exported member 'Config'.

9 const config: Knex.Config = {}
                     ~~~~~~

I expect the fix is basically to run through the typings files and stick export in front of a lot of interface and type decls, but the details of TypeScript typings are not my strong suit, so there might be a less verbose way.

I think 177938afeb2abf7d952f4a16b5a7cde213ab0c17 is where the exports changed.

(Ping @lorefnon for typings.)

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19 (12 by maintainers)

Most upvoted comments

Confirmed Knex 0.95.0 has a peer dep constraint of ^11.0.3 https://github.com/knex/knex/blob/0.95.0/package.json#L60 and the Tedious breaking change happened in v10.0.0 https://github.com/tediousjs/tedious/releases/tag/v10.0.0 so I’m at a loss for how I wound up with new Knex and old Tedious installed without npm yelling at me. But that appears to be the root of my issue.

I hope this helps anyone else running into this issue.

That seems to have done it. Past the typings issue to the tedious issue.

See UPGRADING.md for migration guide - TypeScript version 4.1+ is needed. I think that should resolve your problem!