typescript - 并非所有 braintree HostedFields 方法都可用于 typescript ?

标签 typescript braintree braintree-sandbox braintree-javascript

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

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

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

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

类型“HostedFields”上不存在属性“focus”.ts(2339)

VS Code 也只建议了一些现有的 braintree 方法而不是 .focus():

enter image description here

最佳答案

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

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

关于typescript - 并非所有 braintree HostedFields 方法都可用于 typescript ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64553877/

相关文章:

javascript - 如何获取对象范围之外的回调事件?

json - Angular html,如何为嵌套对象进行 ngfor 循环?

paypal - 通过 Braintree + Braintree Webhooks 从 PayPal Payments Pro(w/h 定期计费)+ IPN 迁移到 PayPal Payments

credit-card - 现代信用卡处理系统是否允许在持卡人姓名中使用变音符号?

javascript - 如何修复最新版本中的 braintree javascript 错误 "t.getVersion is not a function"

javascript - react 和 typescript 上的传播运算符错误

javascript - 布伦特里支付网关 : paymentMethodNonceReceived unable to stop submitting form

stripe-payments - 将应用程序移至生产环境后,是否需要托管 Stripe\Braintree 支付网关的后端服务器?

ios - 我通过 Braintree 获得了 PaymentMethodNonce 字符串,但我似乎无法在服务器端找到它。有什么线索吗?

对象类型的 TypeScript 索引签名隐式具有类型 any