jwt-decode“该表达式不可调用。” 带打字稿

Fid*_*eak 2 jwt typescript react-native expo

刚开始看到这个错误。

我知道这与这里的问题有关:Using jwt-decode with typescript front-end project

然而,我已经在尝试答案,但它不起作用......

在此输入图像描述

代码

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;
}
Run Code Online (Sandbox Code Playgroud)

错误

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)`
Run Code Online (Sandbox Code Playgroud)

注意:我在 React 项目中使用了相同的代码片段,并且运行良好。然而,这似乎打破了我的react-native/expo项目。不确定这是否是与 dom 相关的问题或交易是什么。有替代解决方案吗?

Vas*_*iak 9

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";
Run Code Online (Sandbox Code Playgroud)