【发布时间】:2020-01-23 11:43:53
【问题描述】:
我正在尝试写这种类型:
type Pagination<I extends Iterable> = Readonly<{
seek: number;
limit: number;
total: number;
items: I;
}>;
这样用户就可以使用了:
Pagination<Map<number, any>>
但这似乎不起作用,因为Iterable 还需要一个泛型参数。
所以我只剩下了
type Pagination<I> = Readonly<{
seek: number;
limit: number;
total: number;
items: Iterable<I>;
}>;
虽然这也有效,但类型签名现在变为Pagination<number>,无法进一步限制应该使用哪种可迭代对象。
【问题讨论】:
-
你可以做到
type Pagination<I extends Iterable<any>> = Readonly<{ /*...*/ items: I }>。 -
@TitianCernicova-Dragomir 这就是答案!请为此提交答案。
标签: typescript typescript-generics