作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下 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/
我是一名优秀的程序员,十分优秀!