模块名称隐藏了绑定中使用的全局名称

module name is shadowing global name used in binding

tl;dr: 如何更改以下绑定以能够写入 Intl.DateTimeFormat.make() 而不是 Intl_.DateTimeFormat.make()?

type dateTimeFormat;

[@bs.deriving abstract]
type formatOptions = {
  [@bs.optional]
  weekday: string,
  [@bs.optional]
  day: string,
  [@bs.optional]
  month: string,
};

module Intl_ {
    module DateTimeFormat {
      module Impl {
        type t;
      };

      [@bs.new] external make: unit => Impl.t = "Intl.DateTimeFormat";
      [@bs.send] external format: (Impl.t, Js.Date.t) => string = "";
    };
}

Intl_.DateTimeFormat.make()
  ->Intl_.DateTimeFormat.format(Js.Date.make())
  ->Js.log;

问题

没有下划线,这将编译为:

var Impl = /* module */[];

var DateTimeFormat = /* module */[/* Impl */Impl];

var Intl = /* module */[/* DateTimeFormat */DateTimeFormat];

console.log(new Intl.DateTimeFormat().format(new Date()));

exports.Intl = Intl;

问题是 var Intl = ... 遮盖了全局 Intl,从而破坏了 new Intl.DateTimeFormat()

首先,我认为这是 BuckleScript 中的一个错误。这个问题最近在 issue #3268 and partially resolved in this commit 中被提出,但它仍然留下了很多未保留的名称。您应该考虑在那里或在新问题中提出这个问题。

同时,您可以通过完全限定名称来解决此问题。 Intl 实际上不是全局对象,而是附加到 全局对象,在网页上下文中是 window。但是由于 JavaScript 会在本地环境中找不到名称时在全局对象上查找名称,因此它看起来很像一个全局名称。

因此,如果您将 make 更改为:

[@bs.new] external make: unit => Impl.t = "window.Intl.DateTimeFormat";

它应该可以正常工作。