fastify/fast-json-stringify

Date coercion isn't supported by TypeScript types

tmcw opened this issue · 4 comments

tmcw commented

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.26.0

Plugin version

No response

Node.js version

20.x

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

14.3

Description

The readme has the following code example:

const stringify = fastJson({
  title: 'Example Schema with string date-time field',
  type: 'string',
  format: 'date-time'
})

const date = new Date()
console.log(stringify(date)) // '"YYYY-MM-DDTHH:mm:ss.sssZ"'

This is great and desired behavior. But it doesn't typecheck. Here's a TypeScript playground link for the message

Which is:

Argument of type 'Date' is not assignable to parameter of type 'string'.

Steps to Reproduce

Add a string type with format: date-time as the spec for fast-json-stringify

Expected Behavior

No response

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests. We use tsd for types.

tmcw commented

Yep! Working on one now, a little confused how there appears to already be a passing tsd test for this behavior, will need a little more investigation!

build(schema12)(new Date())

tmcw commented

Figured that out - the type test file isn't testing for anything because all of the schemas are defined with : Schema as their type, which knocks out the inference. I'll rework that.

tmcw commented

PR: #683