1

I am using react-datetime with a react typescript project. I got TS2769 compile eror"

      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>>'.

Following is my code:

...

handleDateChange(inputdate: moment.Moment) {
    ....
}

<DateTime
 timeFormat={false}
 viewMode="days"
 closeOnSelect
 closeOnTab
 required
 onChange={this.handleDateChange}
/>

I am not sure how to declare the type correct to make it compile. Any can help ?

Jun Q
  • 415
  • 1
  • 4
  • 13
  • What are you returning from handleChangeDate? – Remy Oct 24 '19 at 20:38
  • Nothing got returned. Just update the local state of the component. @Remy – Jun Q Oct 25 '19 at 04:59
  • Then I think that might be your problem: `Type '(inputDate: Moment) => void' is not assignable to type 'EventOrValueHandler>'.` You can't assign void to type EventOrValueHandler>'. <-- That's what you need to provide in place of handleDateChange (or return something onChange can handle)? – Remy Oct 25 '19 at 08:22

1 Answers1

0

Your problem is that your handleDateChange doesn't equal what's required by onChange:

onChange?: EventOrValueHandler<ChangeEvent<any>>;

which is of type:

type EventOrValueHandler<Event> = (event: Event | Moment | string) => void;

If you update your function to accommodate for this you should be good. Eg:

<Datetime
    timeFormat={false}
    viewMode="days"
    closeOnSelect
    onChange={
      (value: ChangeEvent | Moment | string) => {
        if ((value) && value as Moment)
          console.log("Do something with moment")
      }
    }
/>
Luke
  • 1