【发布时间】:2020-02-08 17:27:12
【问题描述】:
我有以下type。
type Team = 'liverpool' | 'manUtd' | 'arsenal' | null;
然后我有以下对象。
const teams: Record<Team, JSX.Element> = {
liverpool: <Liverpool />,
manUtd: <ManU />,
arsenal: <Arsenal />,
};
如果我使用teams 作为Record 的第一部分,我会看到以下错误:
Type 'Team' does not satisfy the constraint 'string | number | symbol'.
Type 'null' is not assignable to type 'string | number | symbol'.ts(2344)
如果我使用Record<string, JSX.Element>,它可以正常工作。
【问题讨论】:
-
null不能用作密钥。为什么你的类型定义中需要null? -
感谢 @georg 在应用程序的其他地方它可以为空,因为它并不总是存在:-)
标签: javascript reactjs typescript ecmascript-6