如何在打字稿中指定通用键类型?

时间:2020-10-26 14:50:48

标签: typescript generics

在某些情况下,我有一个泛型查询,可以返回任何类型的集合,并且某些子查询严格期望返回Decks,Cards或Notes Collections,根据名称对卡片组进行索引,并通过cards id,cards ids返回数组并返回注释ID和注释,并返回ID。

export type Collection<K extends (number|string), V extends (number|string)> = { [key: K]: V[] }
export type DecksCollection = Collection<string, number>
export type CardsCollection = Collection<number, number>
export type NotesCollection = Collection<number, string>

它让我An index signature parameter type must be either 'string' or 'number'.

我想正确地写第一行,因为我严格要求使用字符串或数字,但不能同时使用两者,并且K必须是数字或字符串,因为对于打字稿来说,它是一个索引。

0 个答案:

没有答案