You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在TypeScript中引入JS库遇报错:TouchID.isSupported不是函数

解决TypeScript中TouchID.isSupported()不是函数的问题

嘿,我之前也踩过这个坑!这个报错的核心原因是TypeScript对模块导入的规则比JavaScript更严格,再加上react-native-touch-id的导出方式是默认导出而非命名导出,导致你用import * as TouchID导入后,没法正确访问到isSupported方法。

快速修复:修改导入语句

把原来的命名空间导入改成默认导入就好了:

import TouchID from 'react-native-touch-id';

为什么这样能解决问题?

在JavaScript里,import * as TouchID会把模块的所有导出(包括默认导出)都打包到TouchID对象里,所以你可能能间接摸到默认导出的方法。但TypeScript对模块类型的检查更严谨,而react-native-touch-id的核心API是挂载在默认导出的对象上的,不是作为独立的命名导出存在的,所以直接用默认导入才能拿到正确的API对象。

完整代码示例

修改后的代码应该是这样的:

import TouchID from 'react-native-touch-id';
import { AlertIOS } from 'react-native';

// 假设你的authenticate逻辑是这样的
function authenticate() {
  console.log('TouchID认证通过!');
  // 这里写认证成功后的业务逻辑
}

function FingerPrintCheck() {
  console.log('altered');
  TouchID.isSupported()
    .then(authenticate)
    .catch((error: any) => {
      AlertIOS.alert('TouchID not supported');
    });
}

额外检查:确保类型定义正确

如果修改导入后还是有类型提示问题,建议安装对应的TypeScript类型定义文件:

npm install @types/react-native-touch-id --save-dev

可选补充

另外提一句,react-native-touch-id这个库已经停止维护了,官方现在推荐使用社区维护的@react-native-community/touch-id,新项目可以考虑迁移过去,它的API和用法基本一致,还支持更新的React Native版本。

内容的提问来源于stack exchange,提问作者manuelBetancurt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:33:36