-
Notifications
You must be signed in to change notification settings - Fork 465
Add Core
standard library support for genType
#6019
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
996c83c
to
3f37edd
Compare
@@ -201,9 +201,14 @@ let translateConstr ~config ~paramsTranslation ~(path : Path.t) ~typeEnv = | |||
{dependencies = []; type_ = EmitType.typeReactElement} | |||
| (["FB"; "option"] | ["option"]), [paramTranslation] -> | |||
{paramTranslation with type_ = Option paramTranslation.type_} | |||
| (["Js"; "Null"; "t"] | ["Js"; "null"]), [paramTranslation] -> | |||
| ( (["Js"; "Undefined"; "t"] | ["Undefined"; "t"] | ["Js"; "undefined"]), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zth what else from paths hardcoded in this file should be supported for Core, in addition to Undefined.t
and Null.t
and Nullable.t
and Dict.t
and Promise.t
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Things like Option.t
or Int.t
or Float.t
?
I think we're under the assumption that we're under "-open RescriptCore"
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Undefined.t
is getting axed, but the rest of them for sure. Here are a few other candidates (some are perhaps already supported):
- BigInt (https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-2.html#bigint)
- Date
- Map
- Set
- RegExp (?)
- WeakMap
- WeakSet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On my phone now can’t verify but I think undefined<…> and null<…> are at the top level of Core for easy usage in code. Don’t need to prefix with a module name if you open Core. Js.Undefined.t is used frequently in my genType bindings - bummed if the Undefined module is being axed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On my phone now can’t verify but I think undefined<…> and null<…> are at the top level of Core for easy usage in code. Don’t need to prefix with a module name if you open Core. Js.Undefined.t is used frequently in my genType bindings - bummed if the Undefined module is being axed.
Why use Js.Undefined.t<typ>
when you can use option<typ>
? The definition of Js.Undefined
predates the runtime representation of None
as undefined
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was writing bindings yesterday with gentype and the function I was binding to expected an undefined or T but not a null. I think option, which I used first, compiles to null or undefined or T. I always start with option but sometimes it doesn’t work with gentype.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Undefined.t
is getting axed, but the rest of them for sure. Here are a few other candidates (some are perhaps already supported):
- BigInt (https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-2.html#bigint)
- Date
- Map
- Set
- RegExp (?)
- WeakMap
- WeakSet
All supported now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was writing bindings yesterday with gentype and the function I was binding to expected an undefined or T but not a null. I think option, which I used first, compiles to null or undefined or T. I always start with option but sometimes it doesn’t work with gentype.
Can you give an example of what function you were binding to? Did you get a TypeScript type error, or is it a worry that things don't match?
@zth this should be complete now and ready for review. |
Fantastic! Yes I'd be interested in hearing more about what use cases option does not handle from @jmagaram . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice to read some gentype code.
I'm writing bindings for Firebase SDK, a very popular library from Google. These functions require types that are
My binding for the first. I suppose I could create multiple bindings, one with the optional parameter and one without, but this is a hassle. I'd rather create all my friendly overrides in Rescript.
|
I still don't understand why |
I'm confused I thought I just did. Those function declarations are straight from the Firebase SDK. I'm trying to write genType import statements for those. If I use option<FirebaseApp.t> it does not work. I get errors in Typescript saying the type is expecting If you want to try it yourself, put this in a .ts file...
Then try to import it like this and look at the errors in the
There is tricky stuff I can't get my head around here. Like if the ReScript args is |
Right, so this particular issue is that gentype treats |
genType takes the most general approach of accepting both |
Generally speaking, treating |
Here's a PR that treats |
In TS these two types are different: type t1 = {x?: string}
type t2 = {x: string | undefined} in that The two ReScript types corresponding to them, as things currently stand, are: type t1 = {x?: string}
type t2 = {x: Js.undefined<string>} See #6022 as relevant. |
This produces strictly fewer conversion functions, as it does not need to transform `null` to `undefined`.
This is a breaking changes, but aligns more closely with TS. The two TS types: ```ts type t1 = { x?: string }; type t2 = { x: (undefined | string) }; ``` now correspond to the ReScript types: ```res @genType type t1 = {x?: string} @genType type t2 = {x: Js.undefined<string>} ``` The special treatment of fields of option type comes from a time where records with optional fields did not exist.
See https://forum.rescript-lang.org/t/difficulty-with-shims-and-undefined-t-and-null-t/4240