【发布时间】:2022-07-11 23:53:00
【问题描述】:
我正在尝试使用这种类型,但我不能:
type ROLES = "one" | "two"
type Users = {
name: Record<[key in ROLES]?, User[]>;
};
因为它抛出:
Generic type 'Record' requires 2 type argument(s). ts(2314)
为什么?
【问题讨论】:
-
我得到一个不同的错误。你能提供一个Playground 可以看到错误吗?你可能想要这个
name: Partial<Record<ROLES, User[]>> -
错误信息说明了一切。您必须定义两个泛型,因此如果您希望密钥成为角色的密钥,则需要删除
?并使用Record<ROLES, User[]>。如果你想要可选的条目,你可以使用Partial<Record<ROLES, User[]>>。 -
Record 的第一个参数必须解析为字符串,那么您要达到什么目的。
-
院长的回答是。谢谢!请写一个答案,以便我接受。
标签: typescript