Events as records, approach 3. by cknitt · Pull Request #695 · rescript-react-native/rescript-react-native
I had already tried a similar approach except for defining
module ErrorEvent = Event.SyntheticEvent({ type payload = {error: string}; });instead of
module ErrorEvent = { type payload = {error: string}; include Event.SyntheticEvent({ type _payload = payload; }); };and kept getting errors trying to access fields, both with local opens and type annotations. However, as I kept getting errors even with fields such as
nativeEvent, I didn't suspect inline definition ofpayload.
Anyway, I've tried your version and it works. Further, it can be broken with inline definition so there might be a bug with the compiler for this edge case, either it should work or it should lead to an error referring to the inline definition.I just read it, did you try the following:
module ErrorEvent = { type payload = {error: string}; include Event.SyntheticEvent({ type payload; }) with type payload := payload; };This is the syntax to redefine types in an include.
Possibly we can even remove the need for it to be a functor (why is it a functor?)
This would possibly enable the following:
module ErrorEvent = { type payload = {error: string}; include Event.SyntheticEvent with type payload := payload; };Much simpler 😄
That syntax doesn't work for me on 7.3.2 or 8.0.0. Something like
module SyntheticEvent = { type payload; type t = { bubbles: Js.Nullable.t(bool), cancelable: Js.Nullable.t(bool), currentTarget: float, defaultPrevented: Js.Nullable.t(bool), dispatchConfig: registrationName, eventPhase: Js.Nullable.t(float), isTrusted: Js.Nullable.t(bool), target: Js.Nullable.t(float), timeStamp: float, [@bs.as "type"] _type: Js.Nullable.t(string), }; include EventMethods({ type event = t; }); }; module ErrorEvent = { type payload = {error: string}; include (SyntheticEvent: (module type of SyntheticEvent) with type payload := payload); };
works only when t does not depend on payload as above. Once the nativeEvent key is added, it appears we have to override t as well.
Sorry for dropping off the conversation, I've been swamped with a work project in the past weeks, but now I'm free(r) now that I've submitted my part yesterday.