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

consola.log Error: 'date.toLocaleTimeString is not a function' when date is a string #351

Open
GoodWave2020 opened this issue Feb 21, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@GoodWave2020
Copy link

GoodWave2020 commented Feb 21, 2025

Environment

Node: 22.12.0
PNPM: 9.15.1

Reproduction

When running the following code:

import consola from 'consola';

consola.log({ date: 'string' });

the following error occurs:

Error: date.toLocaleTimeString is not a function

This error is triggered because consola expects the date field to be an instance of Date rather than a string.

Describe the bug

When invoking consola.log with an object where the date field is set to a string, the process fails at runtime. Internally, consola calls toLocaleTimeString on the date field, which only exists on Date instances. Passing a string results in the aforementioned error.

return opts.date ? date.toLocaleTimeString() : "";

Additional context

I am attempting to customize the log output by manually specifying a custom date format.

To achieve this, I passed an object with a date field set to a string (i.e., { date: 'string' }). However, this leads to a runtime error because consola expects a Date object.

It would be helpful if either the documentation clarified that only Date objects are accepted, or if consola provided support or a graceful fallback for string values.

This change would allow developers greater flexibility when customizing log output.

Logs

@GoodWave2020 GoodWave2020 added the bug Something isn't working label Feb 21, 2025
@GoodWave2020
Copy link
Author

GoodWave2020 commented Feb 21, 2025

Hi maintainers,

I'd like to propose a fix for this issue.

My plan is to add a check before invoking any Date instance functions (such as toLocaleTimeString).

The code would verify if the provided value is indeed a Date instance, and if it is not, it would simply do nothing.
This should prevent the error without altering the intended functionality.

Would it be acceptable for me to submit a pull request with this change?

Thanks in advance for your feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant