You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 13, 2023. It is now read-only.
Repository navigation
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
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?
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.
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].
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).
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
interfacePerson{name: string};
Then consumers can decide to extend these types outside Reason, knowing that any additional data added to the objects will be preserved.
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 {| ... |}.
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.
Activity
cristianoc commentedon Oct 1, 2018
In terms of trade-offs, those are the positives.
The negatives include:
type t = int, so the price to pay is a bit of inconsistency.Overall, I'm not sure.
nikgraf commentedon Oct 1, 2018
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?
cristianoc commentedon Oct 2, 2018
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.
jchavarri commentedon Oct 21, 2018
One idea would be to only allow to export an
interfacefromJs.tobjects, 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].cristianoc commentedon Oct 22, 2018
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).
jchavarri commentedon Oct 22, 2018
I was referring to the fact that
Js.ttypes are represented by BuckleScript as plain JavaScript objects. So genType doesn't need to apply any conversions to the input or outputJs.ttypes, and thus no data from a potentially extended interface in TS/JS land would get lost. This was referring to your previous comment:So I was thinking something like what @nikgraf proposed but with
Js.ttypes:would produce
Then consumers can decide to extend these types outside Reason, knowing that any additional data added to the objects will be preserved.
cristianoc commentedon Nov 1, 2018
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
exactobject types{| ... |}.cristianoc commentedon Nov 1, 2018
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.