javascript - 如何修复“CheckBoxData []类型上不存在属性'值'”?
问题描述
我最近试图将 React 应用程序从 JS 转换为 TS。在这样做时,我想出了这个问题。以及如何解决这个问题的想法。我转换为 ts 方法错了吗?
我在我的 JS 代码中添加了接口,希望就是这样,但我无法解决这个问题
export default React.memo<CheckBoxProps>(
({
**line23->** data: { value, editable = true, error, label, required = false, name },
focused,
userColor,
listeners
}) => {
const hasError = !!error;
return (
<CheckboxWrapper focused={focused} userColor={userColor}>
<Checkbox
name={name}
isChecked={value}
isInvalid={hasError}
// isDisabled={!editable}
isRequired={required}
label={label}
{...listeners}
/>
{error && <Error>{error}</Error>}
</CheckboxWrapper>
);
}
);
interface CheckBoxProps {
data: CheckBoxData[];
focused: boolean;
userColor;
listeners;
}
export interface CheckBoxData {
value: number;
editable: boolean;
error: string;
label: string;
required: boolean;
name: string;
}
错误显示:TS2339:“CheckBoxData []”类型上不存在属性“值”。
解决方案
我最好的猜测是你需要data: CheckBoxData
代替data: CheckBoxData[]
.
interface CheckBoxProps {
data: CheckBoxData;
focused: boolean;
// type missing in following properties, temporarily fixed with any
userColor: any;
listeners: any;
}
我认为错误消息是非常不言自明的,它具有创建数组的data
类型。因此,该行上的破坏就像 write 一样,数组没有属性,因此会出现错误。CheckBoxData[]
data
data.value
value
推荐阅读
- javascript - 是什么阻止我提交表格?
- azure - 如果安装 ID 未知,如何更新安装?
- javascript - 如何阻止 SignalR 排队警报
- composer-php - CakeDC 用户插件 Composer 安装
- hive - Hive View 查询未使用分区
- javascript - 如何在使用jquery / javascript单击编辑图标按钮时更改选定的行颜色
- get-satisfaction - 如何获取 Shields.io 的 GetSatisfaction 社区的人数?
- c - 为什么递归中的全局变量比局部变量使用更多的内存?
- javascript - 两个异步函数完成后的回调
- swift - 写入数据 Firebase Swift $