NodeJS 当我使用@types/jsonwebtoken的sign函数时,我得到一个错误

brc7rcf0  于 2023-08-04  发布在  Node.js
关注(0)|答案(1)|浏览(129)

当我使用函数符号时:

const token = await sign(
    { userId: user.user_id },
    config.jwtSecret,
    { expiresIn: '2d' }
)

字符串
因为config.jwtSecret是字符串类型,所以错误是:
“string”类型的参数不能分配给“null”类型的参数。ts(2345)
出现此错误的原因是代码:

export function sign(
    payload: string | Buffer | object,
    secretOrPrivateKey: Secret,
    options?: SignOptions,
): string;
export function sign(
    payload: string | Buffer | object,
    secretOrPrivateKey: null,
    options?: SignOptions & { algorithm: "none" },
): string;


我应该如何修复此错误?
我尝试设置配置的类型。jwtSecret是any,但必须加上{ algorithm: "none" }

9o685dep

9o685dep1#

// Assuming config.jwtSecret is of type string | null
const token = await sign(
  { userId: user.user_id },
  config.jwtSecret as Secret | null,
  { expiresIn: '2d' }
);

字符串
使用此格式

相关问题