我无法理解如何解决 newStartDate
和 newEndDate
上的以下错误。我将此代码作为 DatePicker 组件的入门代码的一部分。
当我将鼠标悬停在 newStartDate
和 newEndDate
红线上时看到的错误是:
绑定元素“newStartDate”隐式具有“any”类型。ts(7031)
const handleInputValuesChange = ({
startDate: newStartDate,
endDate: newEndDate
}) => {
setStartInputValue(newStartDate || '')
setEndInputValue(newEndDate || '')
}
const handleDatesChange = ({
startDate: newStartDate,
endDate: newEndDate
}) => {
setStartDate(newStartDate)
setEndDate(newEndDate)
}
screenshot of the code with the error hghlighted
我无法理解的是 startDate: newStartDate
和 endDate: newEndDate
实际上代表什么?我该如何解决错误?我需要将 newStartDate 声明为类型吗?还是变量?在哪里?
答案 0 :(得分:2)
因为您使用的是打字稿,但您没有为 handleDatesChange
的参数定义类型
const handleDatesChange = ({
startDate: newStartDate,
endDate: newEndDate,
}: {
startDate: Date;
endDate: Date;
}) => {
setStartDate(newStartDate);
setEndDate(newEndDate);
};