Skip to content

Can't genType a first class module to share with TypeScript #6023

Closed as not planned
@jmagaram

Description

@jmagaram

According to the docs at genType first class modules I should be able to use syntax like export ... but this doesn't work. So maybe the docs need to change.

I have been struggling with how to share my modules with TypeScript. Originally I was sprinkling @genType annotations all over the place but I couldn't figure it out. I tried decorating modules and members of modules. I tried putting the @genType annotation on the interfaces and on the implementations. Sometimes I used @genType.ignoreInterface. Just couldn't get reliable results. Perhaps part of the problem is I'm using functors. Ideally I could slap a @genType on the final module that was built with a bunch of functors and includes.

And so today I thought maybe I could have a single file called TypeScriptInterop.res and I'd put a bunch of first class modules in there, slap @genType on them, and it would all work. And since I have to manually specify the type of these modules, it would seem like genType has all the information it needs regardless of whether they were built with functors and include statements. But no. Here is a single file. I'm trying to make CompanyName available to TypeScript.

module type T = {
  type t
  type domain
  let make: domain => result<t, string>
  let value: t => domain
}

module Make = (
  C: {
    type domain
    let cmp: (domain, domain) => float
    let validate: domain => result<domain, string>
  },
): (T with type domain = C.domain) => {
  type t = C.domain
  type domain = C.domain
  let value = i => i
  let make = (i: domain) => i->C.validate
}

module CompanyName = Make({
  type domain = string
  let cmp = Js.String2.localeCompare
  let validate = i => {
    let len = Js.String2.length(i)
    len > 1 && len < 20 ? Ok(i) : Error("Wrong length")
  }
})

// Can not use domain := string in place; must define separate module type. Why?
module type CompanyNameType = T with type domain := string

@genType
let companyName: module(CompanyNameType) = module(CompanyName)

And here is the .gen.tsx...

import * as ValidatedBS__Es6Import from './Validated.bs';
const ValidatedBS: any = ValidatedBS__Es6Import;

export const companyName: unknown = ValidatedBS.companyName;

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleOld issues that went stale

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions