typescript - 如何扩展DefinitelyTyped社区定义的函数声明?
问题描述
我在 TypeScript 项目中使用jsonwebtoken库。连同这个库,我导入了@types/jsonwebtoken库来提供类型。在这个库中 jsonwebtoken 的函数verify
声明如下:
export function verify(
token: string,
secretOrPublicKey: Secret,
options?: VerifyOptions
): object | string;
但是我想指定它返回的确切对象,而不仅仅是object | string
例如由以下接口定义的对象:
export interface DecodedJwtToken {
userId: string;
primaryEmail: string;
}
我怎样才能在我的项目中实现它?可以不用类型转换就可以完成,即
const decodedToken: DecodedJwtToken = verify(token, JWT_PRIVATE_KEY) as DecodedJwtToken;
先感谢您。
解决方案
您正在寻找的是模块扩充:
import { Secret, VerifyOptions } from 'jsonwebtoken';
export interface DecodedJwtToken {
userId: string;
primaryEmail: string;
}
declare module 'jsonwebtoken' {
function verify(token: string, secretOrPublicKey: Secret, options?: VerifyOptions): DecodedJwtToken;
}
推荐阅读
- javascript - 在数组中迭代一个数组并显示 playerId、playerName 和 playerCategory
- javascript - SignalR v2 Javascript API 文档在哪里?
- r - 配对图未从闪亮下载
- python - 金字塔+ MySQL
- ios - iOS Swift,“在我的 iPhone 上”文件夹的 url 是什么
- docusignapi - 从 DocuSign Rest API 调用 GetDocument 时如何使用加密
- mysql - 从 ISP 服务器下载 MySQL 数据库到 localhost
- java - 是否有必要在可能被单线程调度程序修改的字段上使用 Java 同步?
- mysql - 如何创建正确的查询?
- ios - Cocoapods 禁止所有警告!不能在 1.6.1 中使用 Xcode 10.2.1 / Swift 5