如何在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>;

相关内容

最新更新