gpt4 book ai didi

javascript - 键入对象 TypeScript 的问题 - 使用带有 null 的预定义类型时

转载 作者:行者123 更新时间:2023-11-30 13:50:35 24 4
gpt4 key购买 nike

我有以下 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>它工作正常。

最佳答案

Record<NonNullable<Team>, JSX.Element>

如果这有帮助,请告诉我。出现此错误是因为 null 不是有效的 javascript 对象键类型。

关于javascript - 键入对象 TypeScript 的问题 - 使用带有 null 的预定义类型时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58336946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com