-
Notifications
You must be signed in to change notification settings - Fork 27
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
Adds missing exports (infamous triplet) #17
Conversation
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.
lgtm
@voxpelli can you confirm if this works for you? |
Is Fastify working for you? |
@voxpelli can you use |
In this scenario, .default is intended to be use: here we have a |
One shouldn't need to change ones code to get type validation working, if it runs, then the types shouldn't tell you that it won't run |
In TS isn't possible to achieve full compliance with all of the scenarios. As I said JS checked by TS is the only one we can't cover with this approach. All of the others, that are more used though, are covered and make the users happier. |
JS checked with TS is one of the core scenarios and one which makes sense for Fastify itself as it is also a JS module. One of the key wins of #16 compared to the fix here is that it actually checks that the types and the code matches: 694d09d As this project is JS rather than TS, the only way to actually ensure that the types and the code matches is to use TS to validate the JS. If any scenario shouldn't be supported, it's the old legacy pre-TS-2.7 non-esModuleInterop scenario, if all three can't be supported at once |
I've added self-hosted types for the modules-playground/ fox1t/modules-playground#3 to allow experimentation and in that PR showed all cases that are supported with the triplet and all cases that are supported with the namespace solution The ones who have import helloWorldRequire = require("../../") |
The problem is that some other packages, that our users depends on, use different esModuleInterop settings. This part of TS is a real mess because of the compiler checking also node_modules deps. We can't just "don't care about esModuleInterop".
What is important to understand (I am repeating this as future reference too) is that if someone writes a TS package and This TS export export default moduleEntryPoint
export { moduleEntryPoint } will be compiled to: module.exports.default = moduleEntryPoint
module.exports.moduleEntryPoint = moduleEntryPoint So also in this case Since we export |
I have to add that I perfectly understand your use case, and if there is any way to cover it without dropping the others, we will do our best to support it. |
Yes, I'm not saying it isn't a breaking change, but as TS itself highly recommends one moving to TS has acknowledged that the pre- As I've shown in fox1t/modules-playground#3 it is possible to get code to work in all using a non-triplet approach (but of course it would be breaking) |
That said, of course this PR enables it to work at all (using the So I guess this should be merged as it brings this project in line with the rest of the Fastify ecosystem, and then we can discuss a possible better next step in fox1t/modules-playground#3 |
This is the real problem with your approach: https://github.com/fox1t/modules-playground/pull/3/files#diff-7fc4abcf18a5d2e9bec68d06cb32eaa705aaa9401e79fbb2a61361f8b91e390cR5 We are forcing our TS users to import the namespace even if we are exporting a perfectly valid faux module. In addition, to that |
Lets have that discussion for that PR, lets merge this one here for now. |
An alternative, even simpler, approach would be to just avoid a top-level export and only have named exports. Only supporting: const { envSchema } = require('env-schema'); And in TS: import { envSchema } from "envSchema"; It's the |
Yes! I agrre with this and it is already supported! :) We stronly encourage all of our users to use named imports/requires. The namespace requires and imports are usual used by CJS users. Thats why we do always 3 exports: we cover all scenarios! :) |
Right, maybe the other ways of importing it could be marked as deprecated then? That will actually get picked up by TS nowadays: https://devblogs.microsoft.com/typescript/announcing-typescript-4-0-beta/#deprecated-support |
Deprecation is too strong for this case: it works perfectly in pure TS projects.
As you can see, all of our users can use the import that is most familiar with, and the one they expect can be used. The only real exception is point 1.1 that forces the explicit use of @mcollina and @Ethan-Arrowood, do you think it might be a good idea to write this recap somewhere to fastify team internal use? |
I would prefer a public blog post or similar that we discuss publicly. This problem is really outside of my comfort zone. |
I suppose I have something to write this weekend! :) |
As titled.
Checklist
npm run test
andnpm run benchmark
and the Code of conduct