Function with "static members"

Hello, I have this js function, how to annotate it with rescript

var thunk = () => { // some code };
thunk.fulfilled = 'fulfilled';
thunk.rejectd = 'rejected';

Could be this impl okay?

@module external thunk: unit => unit = "./thunk"

type thunk = {
 	fulfiled: string,
  	rejected: string,
  	aborted: string,
}

external invokeImpl: (thunk) => 'a = "%identity"

let invoke = (thunk) => invokeImpl(thunk)(.)

I think your code needs a small tweak in order to work:

type thunk = {
  fulfiled: string,
  rejected: string,
  aborted: string,
}

@module external thunk: thunk = "./thunk"

external invokeImpl: thunk => 'a = "%identity"

let invoke = thunk => invokeImpl(thunk)(.)

// Usage
invoke(thunk)

But this is not entirely safe as the following is allowed:

invoke({fulfiled: "", rejected: "", aborted: ""})
invokeImpl(thunk)->Js.Array2.map(x=>x)->ignore

I would do something like this:

type thunk
@module external thunk: thunk = "./thunk"

@get external getFulfiled: thunk => string = "fulfiled"
@get external getRejected: thunk => string = "rejected"
@get external getAborted: thunk => string = "aborted"
external invokeImpl: (thunk, . unit) => unit = "%identity"
let invoke = thunk => invokeImpl(thunk)(.)

// Usage
invoke(thunk)
Js.log(thunk->getFulfiled)
3 Likes

thanks for your response!