-
-
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: Improve typing for min/max #2573
Conversation
No null if at least one date is passed; only null if explicitly no dates are passed.
3dd0b6a
to
2ac7853
Compare
Any live example of this type update, please? |
@iamkun Happy to provide examples; is there a place in the codebase I should do it? For me, the issue the previous PR introduced is this (example code): const myDate = dayjs('2023-08-17');
const anotherDate = dayjs(userInput);
if (!anotherDate.isValidDate()) return null;
return dayjs.min(myDate, anotherDate).add(1, 'week'); With the prior change, TS tells me The function only takes Dayjs objects, but may technically be called with an empty array, in which case it will return
TypeScript doesn't always know whether an array has at least one value:
The types are sorted by specificity (match "has Dayjs" first, match "explicitly empty" second, match "not sure" third) because TS will apply the first match it finds, so you don't want "not sure" at the top or it will always match. Looking at the code now it looks like the function may also accept an explicit min(val: null): null; // explicitly null
min(vals: null | Dayjs[]): Dayjs | null // nullable but not definitely null
max(val: null): null;
max(vals: null | Dayjs[]): Dayjs | null // nullable but not definitely null It does not look like I know some codebases have "type tests" or similar but I couldn't find any here. If you would like something like that can you point me in their direction? I didn't see any in the original PR. |
@iamkun is there anything else I can tell you about this PR, anything else I should do? |
excellent TypeScript example, Thanks. |
@iamkun Could this be released? Would help us out a ton. |
## [1.11.12](v1.11.11...v1.11.12) (2024-07-18) ### Bug Fixes * Add NegativeYear Plugin support ([#2640](#2640)) ([6a42e0d](6a42e0d)) * add UTC support to negativeYear plugin ([#2692](#2692)) ([f3ef705](f3ef705)) * Fix zero offset issue when use tz with locale ([#2532](#2532)) ([d0e6738](d0e6738)) * Improve typing for min/max plugin ([#2573](#2573)) ([4fbe94a](4fbe94a)) * timezone plugin currect parse UTC tz ([#2693](#2693)) ([b575c81](b575c81))
## [1.11.12](iamkun/dayjs@v1.11.11...v1.11.12) (2024-07-18) ### Bug Fixes * Add NegativeYear Plugin support ([#2640](iamkun/dayjs#2640)) ([6a42e0d](iamkun/dayjs@6a42e0d)) * add UTC support to negativeYear plugin ([#2692](iamkun/dayjs#2692)) ([f3ef705](iamkun/dayjs@f3ef705)) * Fix zero offset issue when use tz with locale ([#2532](iamkun/dayjs#2532)) ([d0e6738](iamkun/dayjs@d0e6738)) * Improve typing for min/max plugin ([#2573](iamkun/dayjs#2573)) ([4fbe94a](iamkun/dayjs@4fbe94a)) * timezone plugin currect parse UTC tz ([#2693](iamkun/dayjs#2693)) ([b575c81](iamkun/dayjs@b575c81))
I think it's good to be clear that
null
can be returned frommin
/max
, but TypeScript is powerful enough to be even smarter about this. This PR updates the types so that at least obvious scenarios where at least one date is passed don't then require!
or other code smells.As with all types as they get more complex, it's easy to miss things or forget scenarios. Please let me know if I've missed anything.
I wouldn't actually consider this a "fix" so much as an improvement on existing types but I don't think it's a feature and since the prior revision was marked as a fix, I'm keeping in line with that change.