我正在使用react-datetime和一个react typescript项目。我得到了TS2769编译错误“
TS2769: No overload matches this call.
Overload 1 of 2, '(props: Readonly<DatetimepickerProps>): ReactDatetimeClass', gave the following error.
Type '(inputDate: Moment) => void' is not assignable to type 'EventOrValueHandler<ChangeEvent<any>>'.
Types of parameters 'inputDate' and 'event' are incompatible.
Type 'string | Moment | ChangeEvent<any>' is not assignable to type 'Moment'.
Type 'string' is not assignable to type 'Moment'.
Overload 2 of 2, '(props: DatetimepickerProps, context?: any): ReactDatetimeClass', gave the following error.
Type '(inputDate: Moment) => void' is not assignable to type 'EventOrValueHandler<ChangeEvent<any>>'.以下是我的代码:
...
handleDateChange(inputdate: moment.Moment) {
....
}
<DateTime
timeFormat={false}
viewMode="days"
closeOnSelect
closeOnTab
required
onChange={this.handleDateChange}
/>我不确定如何正确声明类型以使其进行编译。有人能帮上忙吗?
发布于 2020-03-20 03:14:33
您的问题是您的handleDateChange不符合onChange的要求:
onChange?: EventOrValueHandler<ChangeEvent<any>>;它的类型是:
type EventOrValueHandler<Event> = (event: Event | Moment | string) => void;如果你更新你的函数来适应这一点,你应该是很好的。例如:
<Datetime
timeFormat={false}
viewMode="days"
closeOnSelect
onChange={
(value: ChangeEvent | Moment | string) => {
if ((value) && value as Moment)
console.log("Do something with moment")
}
}
/>https://stackoverflow.com/questions/58548597
复制相似问题