3

我在我的 ReactJs 项目中使用 Braintree Sandbox 和 typescript。

根据Braintree Docs for focus,可以使用该.focus()方法聚焦一个字段。

hostedFieldsInstance.focus('number', function (focusErr) {
  if (focusErr) {
    console.error(focusErr); 
  }
});

问题:在我的 typeScript 文件中,hostedFieldsInstance 没有将该.focus()方法显示为有效方法。我收到以下错误:

Property 'focus' does not exist on type 'HostedFields'.ts(2339)

VS Code 也只建议了少数现有的 Braintree 方法,但不是.focus()

在此处输入图像描述

4

1 回答 1

3

DefinitiveTyped 的 TS 定义不包括该函数,但这并不意味着无论如何您都不能调用它。您的选择包括:

  • 在这种情况下绕过 TS 编译器检查 ( (hostedFields as any).focus(...))
  • 为 BrainTree 编写您自己的类型定义,从 DefinitiveTyped 导入现有的并覆盖它们以添加您需要的内容
  • 等待 BrainTree TS 重写(见此评论
于 2020-11-02T06:57:25.180 回答