-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Fix knex d.ts to work with Node16 module mode #5659
Conversation
@OlivierCavadenti - would really appreciate a look at this one. The TypeScript team is really pushing Node users to move to module |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Is it possible to add additional tests which would have been failing under old setup?
@kibertoad I have not found a way to configure the type definition testing tool to test using module node16/nodenext in addition to the current settings. The tool seems to only allow for one tsconfig configuration in the package.json. If someone has a way to do this I am happy to give it a shot. |
Is there plans to release a new patch version soon that includes this fix? |
@valtlai sure, I can publish it tonight |
@kibertoad will this be published this week? Thank you :) |
Does this not resolve #5358? |
It does @Jiralite ! We need a new version published with this code though. |
Awesome. Yeah, I was more pointing out that issue should ideally be closed then. |
Sorry for the delay. 3.0.0 is now out! Please let me know if it works as expected. |
@kibertoad Thank you, it’s now working as expected. Edit: this bug is resolved, yeah, but there is now a entirely different bug: #5706. |
Currently, the knex type declarations use
export default
as if it were an ES module. This has not mattered up until recently with Node's increased ESM support and TypeScript'snode16
module setting, which is the TypeScript's team recommended setting for Node users.Bear with me - this is a bit convoluted. Node's behavior when importing CJS from an ES module is to make
module.exports
the default export. TypeScript adheres to this, which leads to some weird quirks when we look at knex's type declarations. TS properly detects knex as a CJS module, and when it sees a default export in the declarations, it assumes that theknex
function is set tomodule.exports.default
(which it also is) but not tomodule.exports
. So to TS, undernode16
module mode, the default export from knex is simply an object with adefault
property. It cannot be called as a function, even though this is perfectly legal at runtime.What's the fix here? It's quite simple. Use
export =
syntax to describe the CJS export, and declare the circular references set inknex.js
. This works perfectly in both the new world and old, since we are explicitly telling TS the shape of the proper CJS export rather than having it make assumptions.