0

I’m using an internationalization library in JS (https://github.com/format-message/format-message) which contains a function that takes a template literal and an object. The template literals don’t have any interpolated JS variables, but the text has to be wrapped in backticks rather than quotes e.g. when translating ICU-formatted plural variants. Is there a way to type-check template literal parameters of external functions in Bucklescript?

Edit: The code I have currently looks like this:

[@bs.module] external i18n : (string, Js.Dict.t(string)) => string = "format-message";

let _getDisplayText = (~suffix, ~difference) => {
  let differenceStr = string_of_int(difference);
  let i18nMapArg = Js.Dict.empty();
  Js.Dict.set(i18nMapArg, "difference", differenceStr)
  switch (suffix) {
  | Character => i18n([%bs.raw {|`{difference, plural, one {# character} other {# characters}}`|}], i18nMapArg)
  | None => i18n([%bs.raw {|`{difference, number}`|}], i18nMapArg)
  }
};

The signature for i18n() isn't correct - the first parameter should be a template literal string specifically, not a plain string. Can I ensure that the first argument gets transpiled to a JS template literal string in a type-safe way?

glennsl
  • 28,186
  • 12
  • 57
  • 75
Ashok Bhaskar
  • 361
  • 2
  • 16
  • Why does it have to be template literals? I don't see any template literals features actually used, and there are examples in the repo's readme using both template literals and ordinary strings. – glennsl Oct 24 '18 at 20:36
  • 1
    Oh, the source code is pre-processed or read by some tool, and using a template literal instead signifies a different kind of argument? If so, that's pretty hacky and not something you should expect to ever be supported. – glennsl Oct 24 '18 at 20:41
  • 1
    If template literals will become supported, I don't think it'll be on a syntactic level since BuckleScript targets ES5. But it could perhaps generate the equivalent runtime representation. – glennsl Oct 24 '18 at 20:42

0 Answers0