并非所有Braintree HostedFields方法都可用于打字稿吗?

问题描述

我在我的带有打字稿的ReactJs项目中使用Braintree SandBox

根据Braintree Docs进行聚焦,可以使用.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还建议仅使用几种现有的脑树方法,而不建议.focus()

enter image description here

解决方法

The TS definitions from DefinitelyTyped不包含该函数,但这并不意味着您无论如何都不能调用它。您的选择包括:

  • 在这种情况下((hostedFields as any).focus(...))绕过TS编译器检查
  • 为BrainTree编写自己的类型定义,从DefinitelyTyped导入现有的类型定义并覆盖它们以添加所需的内容
  • 等待BrainTree TS重写(请参阅this comment