Allow generating string type instead of "Date"

Issue #26 resolved
itzlegend
created an issue

One of the most obvious uses for TypeLITE is to facilitate class structures which will be used as data transfer objects between javascript and the .Net server environment.

Using TypeLITE for this presents a problem when it comes to the "Date" type.

If json data is hydrated into an object claiming to match the TypeLITE generated interface, any date fields will not be a date - they will be a string (it's not possible to represent a date natively in JSON). Thus for these scenarios it's generally misleading have the "Date" type projected. (TS will think it contains a date object, but in fact it will be a string)

Similarly, if you do populate the property with a "Date" and attempt to send that object up to the server via JSON, a date object is not going to work (it will end up being serialized as a string starting with "function(){.....").

I would like the ability to project any date types as 'string' in the generated typescript. This reflects the truth that it will be a string when it comes over the wire, and prevents any attempt at assigning a real Date object since it won't be serializable without formatting it to an appropriate string format first (of course on the server, I still want JSON.net to deserialize into a datetime).

My current workaround is to post-process the TS generated code and replace all "Date" with "string".

Comments (3)

  1. Lukas Kabrt repo owner

    This feature is already implemented, you can use Convertors for this task:

    <#= TypeScript.Definitions()
            .ForLoadedAssemblies()
            .WithConvertor<DateTime>(t => "string")
     #>
    

    (yeah I know, I have to find some time to update documentation)

  2. Log in to comment