reactjs - 样式化的组件抛出奇怪的打字稿错误
问题描述
我在我的 RN 项目中设置了组件和打字稿的样式
Typescript 在我的一个文件中抛出了一个奇怪的错误,它说Element implicitly has 'any' type because expression of type 'string' can't be used to index 'ColorKeyType'
受影响的代码,错误在return theme.colors[color]
部分
const StyledText = styled(Text)<TextStyleProps>`
color: ${({ color, theme }) => {
if (color) {
return theme.colors[color] || color;
}
return theme.colors.text;
}}
`;
我的主题颜色键入如下:
interface ColorKeyType {
primary: string;
... more colors like the one above
}
然后我使用如下类型:
{
colors: ColorKeyType;
fonts: {
families: {
primary: { [key in FontFamilyType]: string };
};
fontSize: { [key in FontSizeType]: number };
letterSpacing: number;
};
}
提前感谢任何可能的解决方案!
解决方案
将您的颜色类型更改为keyof ColorKeyType
interface TextStyleProps {
...
color: keyof ColorKeyType;
}
推荐阅读
- excel - 在excel中的单元格范围内查找字母数字值
- android - 使用 ConstraintLayout Kotlin 视图超出屏幕
- python - Django:具有多级嵌套的查询集过滤器
- typescript - TypeScript:如何指定需要参数的构造函数的类型?
- javascript - 尝试重定向时出错。ctx.res.redirect 不是函数
- c# - 为什么使用 ASCI 将 char 转换为 int 比使用 int.parse() 解析 char 更快?
- javascript - 方法 fetch post 慢慢地将我的信息添加到状态
- c# - 需要字符串连接帮助
- octave - Octave中的Box Jenkins自相关
- javascript - ReactJS无法读取null的属性“名称”