-
-
Notifications
You must be signed in to change notification settings - Fork 732
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 typing) typescript default export #539
Conversation
(fix typing) typescript default export
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 @firsttris I will merge this, I'd like to find the time to test the typings. Since I don't know TypeScript (yet) I just trust contribution by others, however proposed changes seem to be inconsistent. Could you suggest an easy way to setup a test for the typings?
@@ -3,4 +3,4 @@ | |||
import { DayPicker } from "./DayPicker"; | |||
// We need to use 'export =' to be compatible with the 'module.exports = DayPicker.default || DayPicker' syntax | |||
// in DayPicker.js in the final NPM package. |
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.
I wonder then why @adidahiya added this comment :)
i though about it and there a two solutions: named export and default export. named export: export * from "./DayPicker"
// import would be
import { DayPicker } from "./DayPicker" default export import { DayPicker } from "./DayPicker";
export default DayPicker;
// import would be
import DayPicker from "./DayPicker" named export is better for refactoring, but in your case to stay consistent with JavaScript i would continue with default export
In DefinitelyTyped they create a folder containing a tsconfig.json and the testfile.ts. regards |
You can even do both the default and named export. Imho, that's probably best way to do this. Also I would strongly suggest to export the input picker via the main lib as well. Obviously this one would be a named export. You might have had reasons though to put it in a separate path. Maybe for code splitting? Especially considering the fact that it requires moment to run. That's a big shame tbh and I am hoping #518 will be merged soon making all this easier. |
@@ -3,4 +3,4 @@ | |||
import { DayPicker } from "./DayPicker"; | |||
// We need to use 'export =' to be compatible with the 'module.exports = DayPicker.default || DayPicker' syntax | |||
// in DayPicker.js in the final NPM package. | |||
export = DayPicker; | |||
export default DayPicker; |
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.
This is a breaking change, please don't make it so lightly. There is clear guidance in DefinitelyTyped's README about when to use default exports. See here: https://github.com/DefinitelyTyped/DefinitelyTyped#a-package-uses-export--but-i-prefer-to-use-default-imports-can-i-change-export--to-export-default
As @gpbl stated, there's a reason I left the comment on the line above (which is now invalidated with this code change...).
Can you point me to the code in the distributed react-day-picker
package which suggests that this is the right way to export DayPicker
?
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.
Well it's not about right way per say, it's just it doesn't work at all with typescript. These are only definitions after all. Export default is the standard way of doing it these days. I actually never seen export = something in TS before. It's not as much suggestion as both me and @firsttris encountered a problem that appears to be fixable by changing this. Reading that comment, IMHO that source file should do something like
export DayPicker from 'sourcefile' (afk)
In fact this is how I achieve similar functionality in one of my libs https://github.com/PeterKottas/guestbell-forms/blob/master/src/lib/index.ts with no problems so far. Obviously that's mostly used internally but we haven't encountered problems till now. Maybe I am missing something
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.
it doesn't work at all with typescript
This isn't true; we use it in blueprint with the export =
typings. I've used them in a handful of packages with TypeScript and they work fine. Did you look at the DefinitelyTyped README section which I linked? Was any part of that not clear enough?
Again, I am still curious to the answer to my question above:
Can you point me to the code in the distributed react-day-picker package which suggests that this is the right way to export DayPicker?
Lastly, do you acknowledge that this is a breaking change for typescript consumers, regardless of the fact that it fixes your particular tsconfig + bundling setup?
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.
Yeah that's cool then as long as it works for you. But the behavior we have here is inconsistent. With the documentation. Here http://react-day-picker.js.org/ it says this is the correct way of importing the lib.
import DayPicker from 'react-day-picker';
That's not the case in typescript. Therefore I wouldn't say it's fixing my particular problem. It seems to be fixing the issue where docs are inconsistent with the way it actually works.
Readme makes sense to me, however that doesn't change the fact that it doesn't work.
I agree that it's a breaking change. But in my mind, it's more of a bug fix. It's fixing something that is currently not working.
I am not sure I understand that question to be honest. @firsttris pointed out the code that is causing this problem. It's just the typings, root file for DayPicker. No sure what else are you looking for
Wait @PeterKottas means till now you were using import { DayPicker } from “react-day-picker” ? Friends I don’t use TypeScript here, i just trust you with what you are saying 😀 And it’s sometimes difficult to understand it. As soon as I’ve some time I’ll go into Typescript - but if someone could send a PR which is not a breaking change and with a test showing the things working right ... it would be awesome 😊 Thanks for the heads up everyone |
@gpbl Hey man, at the moment, with this import DayPicker from 'react-day-picker'; I get:
With
I get
With import * as DayPicker from 'react-day-picker'; I get
The only way that works for me out of the box at the moment is const DayPicker = require('react-day-picker'); Hope it makes it clear. I am not exactly sure how many fellow typescripts are using this lib but I would assume they'll run into similar problems. That's why the breaking change might be bit too strong of a statement as as far as I can say, it's broken already. If I go into All works as documented via Which is what this PR does. Hope this makes all clear :) |
totally agree with @PeterKottas basically there are 2 ways: named and default export. to be consistent with the documentation, the API (the way it was implemented) @adidahiya could it be that it works for you in blueprint because you'r using an much older version "react-day-picker": "^5.3.0" with this version everything was fine on my side. the issue occured with your PR from 30.September #487 try to upgrade to version 6.2.1 which includes your change. looking at the typescript documentation and " export = " exsist indeed but its not considered a best practice its just to support the old traditional model
means
which is not consistent with the doc, and introduced a breaking change regards |
@PeterKottas @firsttris there can be many factors at play here; I don't think a simple import statement is enough to demonstrate your problem. Before you make this change, I'd like to see a minimal repro of your problem as an isolated project on Github. Until then, @gpbl, I implore you to revert this PR. Again, this change is obviously breaking and despite the fact that it fixes an import for some build configurations, there are users relying on the old
Can you link me to docs saying that it's not a best practice? AFAIK it's a crucial bit of syntax we need while we are still using CommonJS modules on NPM (most modules are distributed this way). Blueprint is using react-day-picker 5.5.3 (see yarn.lock, not package.json). Since there was not a breaking change to the shape of the I kept asking for evidence in the distributed react-day-picker package, not the documentation, that supports your Line 12 in ae71a0c
I believe what you're looking for in your build setup is a synthetic default export. Try setting |
hey adidahiya, so you expect the user to adjust to your build configuration to get typings working? can you provide an example project where you have 6.2.1 integrated an working? actually your PR broke my build. i even tried with: but i still get the issue i reported here: #533 Looking at the Typescript Modules Documentation you see that
have to be imported like that
which I say is not best partice, because its the old, traditional way |
@gpbl @adidahiya |
Try using Again I think the DefinitelyTyped README gives you sufficient information to decide how to declare this package's types:
As for your PR #542, have you tested that the day picker import actually works at runtime? Or are you only doing a compile-time check? |
typings are used at compiletime, at runtime the code is transpiled javascript. Test with: import * as DayPicker from "react-day-picker" // means all named exports Typescript compiler says:
i dont want to be impolite but more and more it sounds to me that you are not quit sure what we are talking about. we basically need to export this classes: (TypeScript Code) this has nothing to do with the JS Code your refercing. |
Dude @adidahiya you are either somehow misunderstanding the problem or are too proud to admit that your commit needs to be reverted. I am not exactly sure why, obviously NPM docs is not holy bible, things change get updated or removed. You got to roll with the punches. Nobody's pressing any blame, just realize that the lib currently broken! That's the important thing here. People who update from previous version will get their build broken (like @firsttris had ) and will either have to change all imports to undocumented way which is not only difficult, but also against the docs of this library. People who are new users like me will just fail to import it and either give up and use something else (which would be shame) or find this conversation and wonder what the f*** are we discussing here. Any project with relatively recent version of typescript will suffer from this. This is a great lib and I am sure our effort would be better spend than arguing about this simple one liner. For anybody who wants to reproduce this, just use https://github.com/Microsoft/TypeScript-React-Starter and import and you'll see. I've described all errors you'll get in detail in my previous comment. Side note: I am leaving for holidays for a few weeks. I'll keep an eye on this if I can but if not, please not consider me ignoring this ;) Lastly: If there is ANYBODY, who has this working with the latest version (6.2.1) version, please speak up. Only then am I willing to accept the fact that this is a breaking change. Otherwise, it's a bugfix. |
@firsttris those are not the root export; they are submodules in the NPM package. If you look the "main" entry from react-day-picker's package.json, it uses
@PeterKottas Not sure what the "NPM docs" bit is referencing here, can you clarify?
Sure, and I'd be happy to be proven wrong here -- but I don't have the time to produce a minimal repro. It's on you to produce it and link us to a broken build since you are advocating for the breaking change here.
My PR #487 didn't change the shape of the export. There was never an
Then you might want to submit a docs PR to this library :) |
@adidahiya you could have actually saved the time of all your replys by simply cloning TypeScript-React-Starter and doing a simple test... but since you do not even believe the typescript compiler... i give up on explaining to you @gpbl |
Alright, I was able to repro the typings regression in my project. The issue is that we no longer get the namespace declaration merging we had when all the types were in one file, |
I believe confusion comes from how we export the main module here: https://github.com/gpbl/react-day-picker/blob/master/DayPicker.js#L12-L18 This was made some versions ago to keep compatibility with CommonJS when we upgraded to babel 6. I'd like to get rid of this and export the module without specifying the |
When you import a CommonJS Module in TypeScript at development time (in vscode) only the typings are used. The be exact the typings are used by the tsc compiler. ("types" entry from Package.json.) That how you get code completion in TypeScript. While in runtime the JavaScript-Code is executed. So i dont think a JavaScript-Code change will fix the typing. The issue resides in the index barrel file https://github.com/gpbl/react-day-picker/blob/master/types/index.d.ts it should export DayPicker and DayPickerInput Many famous projects just use one index.d.ts file which contains all typings... Project with multiple typing files mostly use Named Export index.d.t.s example
|
@gpbl yes, it would make it easier to write the typescript definitions. 👍 for this change in the next major version.
@firsttris That's correct, but the important thing here is that the TS definition and the JS constructs need to match. It's possible to have completely wrong typings that type check fine, only to produce an error at runtime because the typings did not match the construct of the module you attempt to bundle in webpack. If @gpbl changes it so that |
Awesome! what’s the best thing to do now? I understand It’s OK to release a patch with this PR merged. |
You may understand now that its broken for TypeScript users for about one month. For a quick fix Since the JavaScript package is also exporting the default i don't see the issue But if we think about a TypeScript user using DayPickerInput, it would be best to change your package.json - "main" to a barrel file (maybe with newer module export syntax) to also doing named & default exports similar to the way the typing barrel file would do them. then a TypeScript user could do import DayPicker from 'react-day-picker';
import * as DayPicker from 'react-day-picker'; but also import { DayPicker } from 'react-day-picker';
import { DayPickerInput } from 'react-day-picker'; to get the correct typings. just a suggestion, keep in mind i dont know your full build system |
No, sorry, you still can't do that... I've been trying to explain multiple times on this thread how react-day-picker/DayPicker.dist.js Line 8 in 04e5f2a
|
Like all your other assumtions... this is not correct.
means |
This is getting a little frustrating since it feels like we're talking past each other. I am well aware of the different import & export syntaxes in CommonJS / ES modules and how transpilers work. I am pointing to a specific line in code. This line is executed when you bundle the CommonJS modules of the react-day-picker library through the Now, of course, if you are importing from |
yes because when you you just blocking contributions because you cannot admit that you'r wrong. even the JavaScript docs is doing an default import im out... |
I do admit #487 was a regression (mentioned that above; I was able to repro it #539 (comment)). But this one-line change is not the right fix for all the reasons listed above. |
I dont believe requiring it with default from the module can be fixing an issue from this. |
I am just having the exact same issue ... @gpbl @adidahiya so what fix are you proposing? I mean really, how should we than import using |
@stunaz first I'm adding a line to the docs :) Anyway, we need to fix this ASAP. Since it's not clear to me what's going on, I'd just plan a major release with the right export. I'd like also to remove the common.js default exports which are the root of all the problems 😄 |
(fix typing) typescript default export
This reverts commit e3bc6df.
This PR fixes typescript default import.
For more details see issue #533
regards
Tristan