React Native jwt-decode“This expression is not callable.”使用typscript

hts6caw3  于 6个月前  发布在  React
关注(0)|答案(1)|浏览(87)

刚开始看到这个错误。
我知道这与这里的问题有关:Using jwt-decode with typescript front-end project
然而,我已经在尝试答案,它不工作...
x1c 0d1x的数据

代码

import jwt from 'jwt-decode';

export function isTokenExpired(token: string): boolean {
  // check token payload
  const tokenPayload: any = jwt(token);
  // check token expiration date in payload
  const tokenExpiration = tokenPayload.exp;
  const now = Math.floor(new Date().getTime() / 1000); // token exp shaved off milliseconds
  // if (expiration date is greater than current date)
  return now > tokenExpiration;
}

字符串

错误

This expression is not callable.
  Type 'typeof import("c:/Projects/msal-react-native-expo/node_modules/jwt-decode/build/cjs/index")' has no call signatures.ts(2349)`


注意事项:我在一个react项目中使用了相同的代码片段,它工作得很好。然而,这似乎在我的react-native/expo项目中中断了。不确定这是否是一个dom相关的问题,或者是什么问题。有替代的解决方案吗?

cdmah0mi

cdmah0mi1#

https://github.com/auth0/jwt-decode/blob/main/CHANGELOG.md#migration-to-v400
迁移到v4.0.0
jwtDecode函数现在不再是默认导出,而是作为命名导出提供。请确保在导入此函数的地方更新代码:

-import jwtDecode from "jwt-decode";
+import { jwtDecode } from "jwt-decode";

字符串

相关问题