create-client icon indicating copy to clipboard operation
create-client copied to clipboard

Typescript: DateTime properties are mapped to Date objects but they should be string

Open luca-nardelli opened this issue 6 years ago • 3 comments

As the title says, the typescript interface generator maps the date/datetime/time types to Typescript's Date object (https://github.com/api-platform/client-generator/blob/master/src/generators/TypescriptInterfaceGenerator.js#L47).

However, API Platform by default serializes DateTime objects as ISO 8601 strings, so we should change the type to string I think, otherwise we will run into all sorts of typescript errors when trying to use the date string (which the TS compiler would see as a Date object).

I could do a PR with the change from Date to string, however I have another question: Can the user change how PHP DateTimeInterface is serialized by Api Platform (i.e. from string to timestamp)? If so, how could we tell this to the client generator?

luca-nardelli avatar May 21 '19 21:05 luca-nardelli

There should be a "denormalization" phase in the browser where this date should be transformed from a string to a Date instance imo.

soyuka avatar May 22 '19 07:05 soyuka

I'd say it makes sense if we assume that the interfaces do not represent Api Platform resources but rather how the frontend app denormalizes them.

At the moment, I was using these interfaces to type hint the JSON objects received from the Api Platform backend, if we were to assume this denormalization phase is necessary we would not be able to type hint the responses without having wrong types for date fields, correct?

Personally, I'd prefer having both objects typed, but this means that the generated interfaces should represent how Api Platform serializes (i.e. DateTime is mapped to a string), and then the frontend can declare its own "denormalized" interfaces.

luca-nardelli avatar May 22 '19 10:05 luca-nardelli

I was surprised to find out the JSON date strings are typed as Date, since that never actually happens. Why does API platform assume a conversion?

martijnhartlief avatar Mar 02 '23 13:03 martijnhartlief