question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Knex types not being inferred

See original GitHub issue

Environment

Knex version: 3 Database + version: MSQL OS: Linux

@lorefnon.

Bug

I have 2 projects, both using knex and typescript. Both are open source. One of the two is simply not listening to my Knex table types. My definitions look like this:

declare module 'knex/types/tables' {
  interface Principal {
    id: number;
    identity: string;
    type: number;
    external_id: string;
    nickname: string;
    created_at: number;
    modified_at: number;
    active: number;
  }

  interface OAuth2Client {
    id: number;
    client_id: string;
    client_secret: string;
    allowed_grant_types: string;
    user_id: number;
    require_pkce: number;
  }

  interface Tables {
    oauth2_clients: OAuth2Client;
    principals: Principal;
  }
}

I’ve placed this file in my knexfile.ts, in the file that sets up the knex global singleton, and in a separate knex-definitions.

Elsewhere in my code, I have code like this:

  const result = await db('oauth2_clients')
    .select('*')
    .where('client_id', clientId);

So far result is not inferred as OAuth2Client[], and I’m not sure what else to share to get close to the root of this.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
evertcommented, May 4, 2022

Thank you so much for helping me fix this!!

Reference: https://github.com/curveball/a12n-server/pull/385

0reactions
evertcommented, May 3, 2022

Also no luck, lol. I also tried making sure that I’m consistently importing knex as import knex from 'knex' and remove instances where I did import { knex } from 'knex', because I was worried that somehow Typescript would consider the 2 import paths as separate.

Maybe I just need to look at my other project (where this does work!) and reverse engineer it until I find what’s the difference between the two.

Interesting project btw. Enterprise auth & identity management is something I have been planning to spend time digging more into this year.

Thanks! Well let me know if you ever want a chat about it! I like meeting new people!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Maintaining type inference when aliasing columns in knex
aList is inferred to be of type { primaryId: number, name: string }[] ... aliasing (so long as we only reference the column...
Read more >
Knex Query Builder
We are generally able to infer the result type based on the columns being selected as long as the ... If the query...
Read more >
@types/knex | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
Upgrading from v3 to v4 - MikroORM
This is documentation for MikroORM 4.5, which is no longer actively maintained. ... Without the explicit type, we would infer Object instead of...
Read more >
tgriesser/knex - Gitter
What's the point of using the --knexpath flag if config is not inferred ... Instead I'm getting type errors like Raw<any[]> cannot be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found