typescript - 如何创建一个泛型类型,它是 TypeScript 中其他类型的组合?
问题描述
假设我想创建一个UserResponse
看起来像这样的类型:
type UserResponse = {
data: User,
meta: Meta
}
我怎样才能使Response
类型更通用,以便我不必为只有data
属性类型更改的其他响应类型重新创建相同的结构?
因此,创建一个新类型将如下所示:
type ArticleResponse = Response<Article>
解决方案
就这么简单:
interface Response<T> {
data: T,
meta: Meta
}
type ArticleResponse = Response<Article>;
type UserResponse = Response<User>;
推荐阅读
- azure - Runbook 无法使用命令 New-AzureRmSqlDatabaseExport(对象引用未设置为对象的实例)
- c# - 在实体框架中的事务期间使用主键
- django - Sentry 错误 - 名称解析暂时失败
- php - 在 docker 上安装复制器存档以进行本地 wordpress 开发
- oracle - PowerBI Desktop:间歇性 ORA-03113 错误
- random - 是否可以比 O(n) 更快地生成不同的随机数?
- git - 如何将git添加到多个地方的现有项目
- java - 来自模块的自定义视图未显示在 android studio 调色板中
- c# - 从 Angular 到 Web.API 的 POST 请求被阻止的跨域请求
- spring - Spring Integration Ftp remoteFileTemplate 卡在从 ftp 获取文件上