I try rescript on a little PoC project and in order to have a chance to integrate it in typescript environments, I explore interoperability between the 2 languages.
Everything seemed to work smoothly when I bumped into an issue when handling rescript sum types on the ts side (in fact, on the transpiled js side).
I use ts-pattern in order to pattern-match on the output of a rescript function from typescript (but the exact same problem arises with a standard switch statement).
In my case it’s based on a Belt.Result.t<'a, 'e>.
the generated typescript (gen.ts) gives me a
type MyResult<a,e> =
{ tag: "Ok"; value: a }
| { tag: "Error"; value: e };
so I can pattern match on it (pure typescript side) with
From your edit it looks like you figured it out, but you have a problem here.
type t = Belt.Result.t<int, string> => unit
@module("./index") external idontwork: t = "idontwork"
The problem is that you’re telling the compiler that the external idontwork function takes a Belt.Result.t<int, string>, when really it doesn’t. It takes this thing:
export type resType =
{ tag: "Ok"; value: number }
| { tag: "Error"; value: string };
Which is not the way that ReScript variants are implemented.
thanks @Ryan that’s the kind of issue I was suspecting.
So I guess I should definitely abandon my idea to go back & forth between rescript & typescript like : rescript with gentype → typescript + rescript with interop → rescript.