Skip to content
This repository was archived by the owner on Nov 13, 2023. It is now read-only.
This repository was archived by the owner on Nov 13, 2023. It is now read-only.

Convert Reason record to TypeScript interface with the first letter being uppercased #42

Closed

Description

@nikgraf

This is a duplicate of cristianoc/genTypeScript#4, but I guess since genFlow is the future I post it here again:

In TypeScript it's common that the first character of types and interfaces is upper-case.

Reason

type person = { name: string };

Ideally would be converted to

interface Person { name: string };

Why Interfaces? They offer more capabilities compared to a type declaration e.g. extending them.

Activity

  1. cristianoc commented on Oct 1, 2018

    @cristianoc
    Collaborator

    In terms of trade-offs, those are the positives.
    The negatives include:

    • need to remember to capitalize/uncapitalize the same type when accessed from Reason vs JS.
    • not every type can be made an interface. E.g. type t = int, so the price to pay is a bit of inconsistency.

    Overall, I'm not sure.

  2. nikgraf commented on Oct 1, 2018

    @nikgraf
    CollaboratorAuthor

    Totally can see the capitalize/uncapitalize overhead, but on the other hand for any TypeScript user anything uncapitalize looks surprising.

    Yeah the fact that you can represent an object as type or interface is one of the many things from my pov that make TypeScript not straight forward to use in the day to day.

    My assumption is: If genType doesn't generate idiomatic TypeScript, it will be hard to convince TypeScript teams to adopt Reason. The example I'm thinking about right now is the UI lib I'm working on in Reason. If it generates types that are unconventional it might be the deciding factor for a team to use another library.
    Does this make sense?

  3. cristianoc commented on Oct 2, 2018

    @cristianoc
    Collaborator

    Idiomatic is definitely high up on the priority list.
    I'd just like to explore things a bit further first.

    Interfaces can be extended. When an extended value is passed into Reason and later gets back into JS, it will change representation into Reason records and back to JS objects. This means, that any extra fields will get lost. This could be surprising as typically in JS one would pass the whole object, while crossing the JS/Reason interface means the value is copied.

  4. jchavarri commented on Oct 21, 2018

    @jchavarri
    Contributor

    One idea would be to only allow to export an interface from Js.t objects, as those don't need to be converted on the boundaries. The downside is that it'd require for the Reason author to explicitly annotate the functions where these types are used, but I think it could be worth it as objects seem to be closer in nature to Flow / TS interfaces than records.

    Another (non mutually exclusive) idea is to make the interface generation opt-in with some special annotation like [@genType.interface].

  5. cristianoc commented on Oct 22, 2018

    @cristianoc
    Collaborator

    I'm very interested in exploring interfaces, as that's the one big piece missing for integrating well with the ecosystem and publishing typed libraries.

    I think it's important enough to deserve its own place for design.
    Curious to understand better why things don't need to be converted at the boundaries with interfaces. Is there a simple example of what that means?
    (And perhaps, we can move that to a new issue).

  6. jchavarri commented on Oct 22, 2018

    @jchavarri
    Contributor

    Curious to understand better why things don't need to be converted at the boundaries with interfaces. Is there a simple example of what that means?

    I was referring to the fact that Js.t types are represented by BuckleScript as plain JavaScript objects. So genType doesn't need to apply any conversions to the input or output Js.t types, and thus no data from a potentially extended interface in TS/JS land would get lost. This was referring to your previous comment:

    When an extended value is passed into Reason and later gets back into JS, it will change representation into Reason records and back to JS objects. This means, that any extra fields will get lost.


    So I was thinking something like what @nikgraf proposed but with Js.t types:

    [@genType.interface]
    type person = {. "name": string };
    

    would produce

    interface Person { name: string };

    Then consumers can decide to extend these types outside Reason, knowing that any additional data added to the objects will be preserved.

  7. added
    enhancement
    on Oct 27, 2018
  8. cristianoc commented on Nov 1, 2018

    @cristianoc
    Collaborator

    See this test #81 about how exporting types as interface would look like.

    There are a few questions on how this could be deployed, including whether it should be per-project or even per-instance with a dedicated annotation [@genType.interface].

    In the test, the types are also capitalized, adding a "I" to the name. This is just one possible way. The "I" is a recommendation coming form the TS linter.

    The test also applies to Flow, where one observation is that where interfaces are used, one loses the opportunity to emit exact object types {| ... |}.

  9. cristianoc commented on Nov 1, 2018

    @cristianoc
    Collaborator

    Another observation is that if a type is capitalized when exported as an interface (and perhaps "I"-prefixed), then this becomes visible from JS. It means if a type is changed from object to something simpler, its name changes too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions