Type definition of RangePicker onChange date paramater

3.8k views Asked by At

As per documentation ant design 4.7

onChange    Callback function, can be executed when the selected time is changing   function(dates: [moment, moment], dateStrings: [string, string])

Error

Type '(date: Array<Moment>, dateString: Array<string>) => void' is not assignable to type '(values: RangeValue<Moment>, formatString: [string, string]) => void'.
  Types of parameters 'date' and 'values' are incompatible.
    Type 'RangeValue<Moment>' is not assignable to type 'Moment[]'.
      Type 'null' is not assignable to type 'Moment[]'.

Code

  const dateTimeOnChange = (
    date: Array<Moment>,
    dateString: Array<string>
  ): void => {
    console.log(date);
    console.log(dateString);
  };

Can't find type definition for and design RangePicker onChange 1st parameter type.

1

There are 1 answers

5
Titian Cernicova-Dragomir On BEST ANSWER

From the error message and the docs the problem is pretty clear. The parameters are not arbitrary length arrays, but rather fixed size arrays (aka tuples), also the date parameter may be null

Try

const dateTimeOnChange = (
    date: [Moment, Moment] | null,
    dateString: [string, string]
  ): void => {
    console.log(date);
    console.log(dateString);
  };

You could probably also use date: RangeValue<Moment> if RangeValue is exported from the module it is declared in.