typescript - TypeScript:使用通用扩展?
问题描述
问题:
我试图弄清楚是否可以扩展泛型。
API 的项目和响应
我正在使用具有多种类型项目的 API 服务,例如,如果项目类型为User:
interface IUser {
email: string;
id: string;
displayName: string;
}
当从 API 请求特定项目时,响应是,例如,对于User:
interface IUserResponse {
'@odata.context': string;
email: string;
id: string;
displayName: string;
}
当从 API 请求项目列表时,响应是,例如,对于User:
interface IUserResponseArray {
'@odata.context': string;
value: [ {
email: string;
id: string;
displayName: string;
},
...
]
}
IUser
API 对[非通用]的响应
采取这两个响应,都具有以下基础:
interface IResponseBase {
'@odata.context': string;
}
从这个基础扩展,IUser
响应接口重构:
interface IUserResponse extends IUser, IResponseBase {}
interface IUserResponseArray extends IResponseBase {
value: IUser[]
}
API 的响应泛型
其次,因为这个 API 支持多种类型的项目,所以响应接口应该是通用的。
做一系列通用项目,使IUserResponseArray
通用:
interface IResponseArray<T> extends IResponseBase {
value: T[];
}
问题type name expected
我怎样才能做到IUserResponse
通用。以下定义泛型的尝试无效,来自我的 IDE 的错误提示是type name expected
:
interface IResponse<T> extends ???<T>???, IResponseBase {}
解决方案
推荐阅读
- python - 为什么我在熊猫中遇到属性错误?
- python - 如何使用 python sdk 将 blob 上传到带有子目录的 azure 存储容器中?
- python - VSCode 为我提供旧版本包的文档,但使用新版本的代码
- spring - 在 Spring MVC 应用程序中实现异常处理的最佳位置
- c# - 使用团队中的机器人框架向群聊发送消息
- python - 我想在 python 中循环我的文件创建者一定次数
- expression - 使用 mapbox 表达式获取要素 id
- node.js - 如何在 EJS 模板中转换 blob 对象
- java - 为什么我们要在android中使用RequestQueue?
- php - 如何将索引数组转换为关联数组