-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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: Significantly improve Duration
types
#1338
Conversation
Codecov Report
@@ Coverage Diff @@
## dev #1338 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 176 176
Lines 1725 1725
Branches 394 394
=========================================
Hits 1725 1725
Continue to review full report at Codecov.
|
Thanks. Will the syntax require the latest Typescript run time? Cause I can see some of our users still using very old typescript like 1.x |
Thank you for noticing that, but Day.js already uses Line 414 in fe5f1d0
And unknown type, that was introduced in TypeScript 3.0Line 412 in fe5f1d0
But it is not a big problem because if TypeScript does not understand something it will fallback to any type.Just compare computed variant of extend function:
// TypeScript 1.8.10 (TypeScript doesn't understand this Generic so it fallbacks to `any` type)
function extend<T, unknown>(plugin: any, option?: T): Dayjs
// TypeScript 2.9.2 (TypeScript still doesn't understand `unknown` type)
function extend<T = any>(plugin: PluginFunc<T>, option?: T): Dayjs
// TypeScript 3.0.0 and higher
function dayjs.extend<T = unknown>(plugin: PluginFunc<T>, option?: T): Dayjs So the answer is: It will work, but with fallbacks to type plugin.CreateDurationType = ((units: any) => Duration) & ((time: number, unit?: any) => Duration) & ((ISO_8601: string) => Duration) And only if the user uses TypeScript 2.x all the types will work correctly. |
LGTM |
Thank you, TS expert. Just wondering if you could help us with another TS issue if you got some time. |
## [1.10.4](v1.10.3...v1.10.4) (2021-01-22) ### Bug Fixes * Correct handling negative duration ([#1317](#1317)) ([3f5c085](3f5c085)) * Improve `Duration` types ([#1338](#1338)) ([4aca4b1](4aca4b1)) * parse a string for MMM month format with underscore delimiter ([#1349](#1349)) ([82ef9a3](82ef9a3)) * Update Bengali [bn] locale ([#1329](#1329)) ([02d96ec](02d96ec)) * update locale Portuguese [pt] yearStart ([#1345](#1345)) ([5c785d5](5c785d5)) * update Polish [pl] locale yearStart ([#1348](#1348)) ([e93e6b8](e93e6b8)) * Update Slovenian [sl] relativeTime locale ([#1333](#1333)) ([fe5f1d0](fe5f1d0))
🎉 This PR is included in version 1.10.4 🎉 The release is available on: Your semantic-release bot 📦🚀 |
## [1.10.4](iamkun/dayjs@v1.10.3...v1.10.4) (2021-01-22) ### Bug Fixes * Correct handling negative duration ([#1317](iamkun/dayjs#1317)) ([3f5c085](iamkun/dayjs@3f5c085)) * Improve `Duration` types ([#1338](iamkun/dayjs#1338)) ([4aca4b1](iamkun/dayjs@4aca4b1)) * parse a string for MMM month format with underscore delimiter ([#1349](iamkun/dayjs#1349)) ([82ef9a3](iamkun/dayjs@82ef9a3)) * Update Bengali [bn] locale ([#1329](iamkun/dayjs#1329)) ([02d96ec](iamkun/dayjs@02d96ec)) * update locale Portuguese [pt] yearStart ([#1345](iamkun/dayjs#1345)) ([5c785d5](iamkun/dayjs@5c785d5)) * update Polish [pl] locale yearStart ([#1348](iamkun/dayjs#1348)) ([e93e6b8](iamkun/dayjs@e93e6b8)) * Update Slovenian [sl] relativeTime locale ([#1333](iamkun/dayjs#1333)) ([fe5f1d0](iamkun/dayjs@fe5f1d0))
## [1.10.4](iamkun/dayjs@v1.10.3...v1.10.4) (2021-01-22) ### Bug Fixes * Correct handling negative duration ([#1317](iamkun/dayjs#1317)) ([3f5c085](iamkun/dayjs@3f5c085)) * Improve `Duration` types ([#1338](iamkun/dayjs#1338)) ([4aca4b1](iamkun/dayjs@4aca4b1)) * parse a string for MMM month format with underscore delimiter ([#1349](iamkun/dayjs#1349)) ([82ef9a3](iamkun/dayjs@82ef9a3)) * Update Bengali [bn] locale ([#1329](iamkun/dayjs#1329)) ([02d96ec](iamkun/dayjs@02d96ec)) * update locale Portuguese [pt] yearStart ([#1345](iamkun/dayjs#1345)) ([5c785d5](iamkun/dayjs@5c785d5)) * update Polish [pl] locale yearStart ([#1348](iamkun/dayjs#1348)) ([e93e6b8](iamkun/dayjs@e93e6b8)) * Update Slovenian [sl] relativeTime locale ([#1333](iamkun/dayjs#1333)) ([fe5f1d0](iamkun/dayjs@fe5f1d0))
## [1.10.4](iamkun/dayjs@v1.10.3...v1.10.4) (2021-01-22) ### Bug Fixes * Correct handling negative duration ([#1317](iamkun/dayjs#1317)) ([3f5c085](iamkun/dayjs@3f5c085)) * Improve `Duration` types ([#1338](iamkun/dayjs#1338)) ([4aca4b1](iamkun/dayjs@4aca4b1)) * parse a string for MMM month format with underscore delimiter ([#1349](iamkun/dayjs#1349)) ([82ef9a3](iamkun/dayjs@82ef9a3)) * Update Bengali [bn] locale ([#1329](iamkun/dayjs#1329)) ([02d96ec](iamkun/dayjs@02d96ec)) * update locale Portuguese [pt] yearStart ([#1345](iamkun/dayjs#1345)) ([5c785d5](iamkun/dayjs@5c785d5)) * update Polish [pl] locale yearStart ([#1348](iamkun/dayjs#1348)) ([e93e6b8](iamkun/dayjs@e93e6b8)) * Update Slovenian [sl] relativeTime locale ([#1333](iamkun/dayjs#1333)) ([fe5f1d0](iamkun/dayjs@fe5f1d0))
This PR improves Duration plugin types.
Why?
New types
I also added
CreateDurationType
to reduce code duplication, soduration()
,duration.add()
andduration.substract()
using the same function signature.Other changes you can view in diff. Please, let me know if don't understand something. I can explain every line of code.
UPD: With this PR TypeScript users can't use functions
duration()
,duration.add()
andduration.substract()
without args any more. But I don't think it's a breaking change, because I can't find any example usage in docs for this. In this way we just get empty duration object.P.S. It also would be good to update TypeScript in devDependencies to latest version.