Need API advice for a little TS & ReScript library

Hello, hello,

I create a little state management library (in the mindset of Overmind.js which I enjoyed a lot) but am stuck with a some design decisions regarding TS / ReScript.

A familiar API for ReScript would be:

// *************** ReScript use
let tree = Libname.make({count: 0, total: 0})

// And in some @react.component
let c = Libname.use(tree)

But in TS, one would expect:

// *************** TypeScript use
import { libname, useLibname } from "@libname/react"

const tree = libname({count: 0, total: 0})

// and in a react component
const t = useLibname(tree)

I have no experience publishing ReScript libraries (I am not even aware of what is published: .res sources or .resi and js ?).

How can I build the two librairies from the rescript files but not expose useLibname or libname in ReScript or use and make in TypeScript ?

1 Like

One way to do it is with @genType.as:

// Libname.res
@genType.as("libname")
let make = ...

@genType.as("useLibname")
let use = ...

Bear in mind that that @genType.as feature is deprecated because in the future (V12?) we want genType to not create any runtime code.

But you would probably keep some entrypoint index.js/ts file anyway, where you could just re-export what the user should consume (ReScript won’t look into your index.js).

Other than that I think @DZakh knows more about publishing for both langs.

EDIT: I also found this post which might be useful: Distribute npm package for non-ReScript users - #10 by DZakh

2 Likes