question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Date.prototype.toJSON has incorrect return type

See original GitHub issue

Bug Report

Date.prototype.toJSON() can return a string or null according to the spec https://tc39.es/ecma262/#sec-date.prototype.tolocaledatestring. For example new Date("").toJSON() returns null.

Currently, the return type of toJSON() is specified as a string: https://github.com/microsoft/TypeScript/blob/131875bb849c0a9c56c26d09881453aace0cbfed/lib/lib.es5.d.ts#L879

🕗 Version & Regression Information

Typescript 4.2.3

💻 Code

new Date("").toJSON().slice(0,5);

produces an error at runtime since new Date("").toJSON() is null, but doesn’t produce a typescript error because new Date("").toJSON() is assumed to be string.

🙂 Expected behavior

TypeScript should warn about the potentially null value when an arbitrary string is passed into Date. However, if the Date object can be guaranteed to be valid (for example new Date()), the return type of toJSON() should be string.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
andrewbranchcommented, Apr 14, 2021

Unfortunately, there’s not a super clean, easy way to make a built-in type wider than it’s already declared 😕. This sort of thing makes me want to revisit the idea of having “strict” variants of lib files that people could opt into with the lib compiler option, so we could make these kind of changes without breaking the world.

I guess the easiest option I would recommend is just writing a wrapper function around built-ins whose types are too loose, and always use your wrapper functions instead of the built-ins (you could enforce this with an ESLint rule):

export function dateToJSON(date: Date): string | null {
  return date.toJSON();
}
0reactions
Jooztycommented, Dec 6, 2021

Would be really nice to have correct return types in toJSON() and JSON.strigify(). Today we run into a really bad bug on production because of this. I would expect that TS warned us. Sadly that was not the case. 😢

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript Date toJSON() outputting incorrect date
prototype.toJSON says that it uses Date.prototype.toISOString, which states that "the timezone is always UTC".
Read more >
Date.prototype.toJSON() - JavaScript - MDN Web Docs - Mozilla
The toJSON() method returns a string representation of the Date object. ... primitive is not a number or is a finite number, the...
Read more >
The Date.toJSON method returns an incorrect date string
The Date.toJSON method returns an incorrect date string ... This behavior is caused by the Date.prototype.toJSON() method.
Read more >
JSON date format: 3 ways to work with dates in JSON
2, // WARNING: this is a bad demo, don't copy it, you shouldn't replace native prototype methods ; 3, Date.prototype.toJSON = function ()...
Read more >
JSON and the stringification oddities in JavaScript
In JavaScript, the way to convert a value to a JSON string is via ... For other built-in object types (except for Function...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found