0

I want to update handleChange event as 'CalendarChangeParams', not as 'any', when I do so, I cannot update state which keeps values of picked dates. When I hover over below marked fields: enter image description here

Typescript throws me error as below:

const newValues: Date | Date[] Element implicitly has an 'any' type because expression of type '0' can't be used to index type 'Date | Date[]'. Property '0' does not exist on type 'Date | Date[]'.ts(7053)

I cannot figure out what is wrong here as event is actually an array with Date

type OnChangeType = {
  startDate: Date;
  endDate: Date;
};

type CalendarProps = {
  startDate: Date;
  endDate: Date;
  onChange: ({ startDate, endDate }: OnChangeType) => void;
} & CalendarComponentProps;

export const Calendar: FC<CalendarProps> = (props) => {
  const { selectionMode, onChange, startDate, endDate, ...rest } = props;

  const [values, setValues] = useState<Date | Date[] | undefined>([startDate, endDate]);

  const handleChange = (e: CalendarChangeParams) => {
    const newValues = e.value;
    if (!newValues) return;
    setValues(newValues);

    onChange({
      startDate: newValues[0],
      endDate: newValues[1],
    });
  };

thanks

marcinb1986
  • 708
  • 1
  • 11
  • 30
  • Can you create stackblitz for this problem? It would be good to see the demo and check the problem on the fly. – Hemant Sankhla Jul 26 '22 at 14:23
  • 1
    Also did you try to add "Date | Date[] " in your OnChangeType type? Because it seems you are accessing the "0" index and the type indicated its an Date string. May be this can help please try once. – Hemant Sankhla Jul 26 '22 at 14:28

0 Answers0