Migration to TypeScript #1932
Replies: 3 comments 5 replies
-
This line made my day ❤️. I love to use date-fns and seeing it lagging behind moment in performance is kinda sad. But anyways, I think v3 is gonna be 🔥. |
Beta Was this translation helpful? Give feedback.
-
Yay! I think this is a really good move, and I'm also excited to see it happen. 🎉
Agree. But, I guess we will not be heading to a-very-advanced TypeScript approach (like generics and stuff)? So probably it's not going to be that... high (?) What do you think? |
Beta Was this translation helpful? Give feedback.
-
@kossnocorp With this move, date-fns current ships flow definitions for consumers of the lib. Is the expectation that you will continue to ship these or will we need to migrate to defs coming from flow-typed eventually? |
Beta Was this translation helpful? Give feedback.
-
TL;DR; from this moment, the date-fns codebase is migrating to TypeScript. It's going to be a gradual transition, so all new code has to be written in TypeScript. It will help make the library for lightweight, simplify the maintenance and make typing definitions robust.
Why?
In v2, we introduced many safeguards to help people use date-fns correctly. We added runtime checks of arguments. We've removed implicit string conversion that was an endless stream on complaints and mistakes.
Unfortunately, it didn't help, and people kept converting incorrect strings, this time outside of date-fns. On top of that, these safeguards made the library more heavyweight. Even though we put a lot of effort into optimizing the minimal build, which now is mere hundreds of bytes, this is the ceiling of what we can do. It also adds unnecessary performance overhead.
Another issue is that all that code is only helpful to people that are mistaken and only at the moment when they are mistaken and force others to do extra steps.
There's a better way. Developers that want to have type safety opt to use TypeScript and Flow. The others don't mind occasionally failing and don't want to be told what arguments (i.e., strings 😉) they can't use.
Unfortunately, our TypeScript definitions don't live up to expectations. Many types, like callbacks, are shallow, the definitions themselves are unnecessarily heavy and add more work to the TypeScript server and make the npm package bulky.
The main reason for that is that we generate definitions from JSDoc, and there's only so much we can do. Another issue is that we've written the definitions generation code when we had no idea how TypeScript works and how good types look like. Even though the build system is an impressive piece of software, it adds a toll on maintenance.
What's next?
From now on, all the new code must be written in TypeScript. The existing functions will be gradually migrated.
Once the migration is complete, we'll switch gears and remove all safeguards, switch the build system to use the TypeScript definitions as the source of truth instead of JSDoc and release v3.
But it's still a long way before we can call it a day. Besides migrating the source code, we have to:
It's all a ton of work, and your help is not only welcome, but much needed. Rewrite a function, improve a doc, contribute ideas.
Why not migrate all at once?
That is the question we've asked ourselves when we just started thinking about the problem. The answer was "why the hell not," and we did indeed attempted that. It didn't take much to convert a big chunk of the code to TypeScript, but it was tedious to polish the changes, fix endless type errors, and it was only beginning because before release, we had to adjust the build system. And all that given that PRs are keep coming meaning, more code to convert, adapt, and rebase.
To avoid the situation we did put ourselves with v2, I decided to opt for gradual migration.
It will take more time before we finish and will be able to benefit from the migration, but it will ensure that we can proceed with adding new functions, introducing fixes, and not burning out in the process.
Recap
After we have done, date-fns will become lighter, faster, and safer.
Unfortunately, it will raise a bar for new contributors not familiar with TypeScript, but that's a tradeoff I think worth making.
Please don't hesitate to ask any questions or sharing your opinion.
Beta Was this translation helpful? Give feedback.
All reactions