JS String Enums and bindings

Hi!

I am binding an external module, specifically a function that accepts 3 possible string values at runtime, I want to type it correctly by only allowing those 3 values, but I can’t find a way to do it.

This is what I have right now, which works but its type is string, not environment.:

@deriving(jsConverter)
type environment = [
  | @as("dev") #Dev
  | @as("stage") #Stage
  | @as("prod") #Prod
]

type configuration = {environment: string} // It should type `environment`

@module("data-sdk") external setConfiguration: configuration => unit = "setConfiguration

setConfiguration({
  environment: environmentToJs(#Stage),
})

It is there a way where I can use a type different than string for the environment key?

Thanks!

Edit: The data-sdk dependency is an ES6 module, which also have TS .d.ts typings available.

Since polymorphic variants are compiled to strings anyway, why would you want to use jsConverter for this?

type environment = [
  | #dev
  | #stage
  | #prod
]

type configuration = {environment: environment}

@module("data-sdk") external setConfiguration: configuration => unit = "setConfiguration"

setConfiguration({ environment: #stage })

Playground link

3 Likes

Thank you! Will read more about these polymorphic variants :slight_smile:

1 Like