Spread incoming type in module function

This might be stretching it but can I do something like this

module BaseCollection = (
  T: {
    type data
  },
) => {
  type t

  type extendedData = {
    created: string,
    updated: string,
    // Error on this line
    ...T.data
  }

  @send
  external getList: (t, int, int) => promise<array<'data>> = "getList"

  @send
  external create: (t, 'data) => promise<'data> = "create"
}

Usage:

type notificationSubscription = {
  user: string,
  active?: bool,
  endpoint: string,
  auth: string,
  p256dh: string,
}

module NotificationSubscriptions = {
  include Pocketbase.BaseCollection({
    type data = notificationSubscription
  })
}

Can I somehow tell ReScript type data is a record?

As far as I know, you can’t define something as “any” record. I asked a similar question some time ago, but can’t find the thread anymore.

1 Like

well you actually need to know more about this type than just being a record, it also needs not to have any fields created and updated that are not string. So it’s actually not that easy to model type-wise!

I use ReScript Schema for this. See data in the Stripe integration I recently built GitHub - enviodev/rescript-stripe: đź’¸ Stripe Billing as a Config

The data is a wrapper around S.object schema which you can also use to describe transformations and field renamings. And after you got the schema, I can use it for decoding/encoding + get a list of fields and do some additional logic around this. Checkout the rescript-stripe source code to see how it’s done.

Also, I do the same for rescript-rest.