Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use it properly? #25

Open
vencho-mdp opened this issue Oct 11, 2022 · 3 comments
Open

How to use it properly? #25

vencho-mdp opened this issue Oct 11, 2022 · 3 comments

Comments

@vencho-mdp
Copy link

Hi! In the readme is shown clearly how this library can generate types, but how do we use them to incorporate them into our app?

@snipd-mikel
Copy link

I have done the following to have the types available when using knex, based on this Stackoverflow answer: https://stackoverflow.com/questions/67034024/knex-js-table-infered-typescript-types

Create a directory for your custom types, (i did src/@types) an add all your .d.ts (also should work with standar .ts)

Then, in your tsconfig.ts add the following inside compilerOptions

"typeRoots": ["./nodemodules/@types", "./src/@types"]

then inside your custom types directory create you declaration file for knex Tables, in my case src/@types/index.d.ts

import * as db from './generated/db';

declare module 'knex/types/tables' {      
  interface Tables extends TablesType {
  }
}

type TablesType = {
  [table in keyof db.Tables]: db.Tables[table];
}

@vencho-mdp
Copy link
Author

vencho-mdp commented Jan 27, 2023

Hey! Thank you for answering @snipd-mikel ! Just with that all the autocompletions should be working? How would you generate those types automatically?

@snipd-mikel
Copy link

Hi @vencho-mdp. After using that fix, the typings for simple select, insert and update statements work fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants