How to type object with unknown properties?

In typescript:

interface SomeObj {
  [key: string]: boolean
}

How to type it with Reason/Rescript?

JavaScript objects are dynamic and troublesome enough that it’s hard to start from the shape and find a general way of binding to it. It’s much more effective to start from the use-case and bind to those bottom-up style. If you can provide a snippet of how you’d want to use that object, I can provide the bindings here.

For example I have ts code as below:

// typings
export interface Resource {
  [language: string]: ResourceLanguage;
}

export interface ResourceLanguage {
  [namespace: string]: ResourceKey;
}

// ts code
import i18next from "i18next";
import { langA, langB, langAResources, langBResources } from "./resources";

let options: { resources: Resource } = {
  resources: {
    [langA]: langAResources,
    [langB]: langBResources,
  },
}

i18n.use(someContext).init(options);

langA/langB are just string variables, they may be changed later, not sure how to make bindings for the init options.

Hey @alcheung.

If you might or might not add keys to an object and you don’t know the length upfront, you’re looking at a dictionary. TypeScript’s {[language: string]: ResourceLanguage} is also the ReScript equivalent of the “dictionary” pattern.

You can look at this sample code, for an example. I put the corresponding TypeScript in comments for comparison.
https://rescript-lang.org/try?code=PTAEFMA8AcHsCcAuoCWA7R54DMCGBjcUAJXAGdYBXeQgGVzQHNLdGiBvAKBFFAG00uALbloBcAC5QZRPHSMAulNIVqhANLgAngG5uYAL6dEW6EXjkqNcPSYs2oALygAUmQB0AERT5E7xAA8MnJMAHyc+hAwCMjomDjiJJZqHJH8ADYMzKyS0rLySkmq1rbZbHo8Rsam5snWTq4e3r7+ARbFdFn24OGRKEJwSKDsoJlMAIIANKNZAELTY4zjKlaEZAtzKylkoAag2PCwQqAARO7A7avkJ3oAAgBuuOmgt0KwACaU6eAAFGcXdTWJwAlJwoPFBM9FuMpMF5A0TtCThEHk8Xm9Pt8-udLikQWDIBC0YtZrD8kwESTkZxUc9Xh8vr9-rjrGR8eCsJCZhMtqypCzOnYcpSsstAdcUY86RjGdiAR1wOzCZziZtxWR+eLSt0RUxZrygREeN9kLBoIgULA0BrhqABeRlOLdg0uDw7eqpK6wLwMqLCtCDeRJmleHwSf61Qr1mkDMHKpwTaAzRarTtnFxeCd7WzPZwfaNwMhsw03F4fH5wAMTD9QfnswBaUKl5p+MiFn7QjY89W1n0NptNcvuNuIDtzLuMfU9vN99Uz2OcKo8FAARgAHGh3JQ2z8KCIAMJWzCE4HudAoUfJy3W4F6apmVDrtCStG3Mj4M1M1cbpVE57b3JKDQC8nFCR8NwRADqTfcA0HeAk-1QYDECkH5vzQaZ2CzdUTk1BUDGBUDQCAkDnBOc9EGpACa0bCifivVNQSAA

1 Like

Thx @Maxim! That’s exactly what I’m look for.

you can also use a Map or a Set