Skip to content
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 for issue #6202: Append 'null' as Return Type for toJSON Method in TypeScript Definitions #6221

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

utf26
Copy link

@utf26 utf26 commented Mar 21, 2024

This pull request addresses the issue raised in #6202, where the current TypeScript definitions for the toJSON method do not accurately reflect the possible return value of null for invalid date instances. As documented, the toJSON method returns a string representation of the moment object when valid and null when the moment object represents an invalid date. However, the existing TypeScript type definitions only indicate a string return type, leading to potential type mismatches and runtime errors in TypeScript applications.

Changes Made

  • Updated the TypeScript definitions for the toJSON method to include null as a possible return type. The method now correctly reflects its return type as string | null, aligning with the actual behavior of the toJSON method.

Impact

  • This change ensures that TypeScript users of moment.js have accurate type information, preventing type-related runtime errors when dealing with invalid date instances. It reinforces the reliability of TypeScript definitions in the moment.js library.

Testing

  • Added new test to verify that TypeScript correctly recognizes the updated return type of the toJSON method. This test ensure that no type errors occur when a moment object is invalid, and toJSON returns null.
  • All existing tests have been run to ensure that this change does not affect other functionalities or introduce regressions.

Additional Notes

  • This change is focused solely on updating the TypeScript definitions to match the current JavaScript implementation of the toJSON method. No changes were made to the method's implementation.

By accurately reflecting the toJSON method's behavior in the TypeScript definitions, this pull request aims to improve the development experience for TypeScript users of moment.js, ensuring type safety and consistency with the library's runtime behavior.

Resolves #6202.

Copy link

CLA Not Signed

@abenhamdine
Copy link

It will probably break a lot of users codes, event if it's correct
see the previous similar case, which has been reverted : a52ffb2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

toJSON() on invalid dates produces null, although the type definition says that it always returns string
2 participants