Аргумент типа не может быть назначен параметру типа SetStateAction - PullRequest
0 голосов
/ 07 августа 2020
У меня очень неприятная небольшая проблема, в форме выбора с ReactJS и TypeScripts с использованием useState она возвращает следующую ошибку:
Argument of type '(ScheduleProps[] | { id: string; week_day: number; from: string; to: string; })[]' is not assignable to parameter of type 'SetStateAction<ScheduleProps[]>'.
  Type '(ScheduleProps[] | { id: string; week_day: number; from: string; to: string; })[]' is not assignable to type 'ScheduleProps[]'.
    Type 'ScheduleProps[] | { id: string; week_day: number; from: string; to: string; }' is not assignable to type 'ScheduleProps'.
      Type 'ScheduleProps[]' is missing the following properties from type 'ScheduleProps': id, week_day, from, to  TS2345

    51 |     });
    52 | 
  > 53 |     setScheduleItems(updatedScheduleItems);
       |                      ^
    54 |   }
    55 | 
    56 |   function handleCreateClass(e: FormEvent) {

Ошибка в этом возврате из "setScheduleItems (updatedScheduleItems)", это часть моего фрагмента кода:

interface ScheduleProps {
  id: string;
  week_day: number;
  from: string;
  to: string;
}

const [scheduleItems, setScheduleItems] = useState<ScheduleProps[]>([
    {
      id: newId(),
      week_day: 0,
      from: '',
      to: '',
    },
  ]);

  // Update "week_day", "from" and "to"
  function setScheduleItemValue(
    position: number,
    field: string,
    value: string,
  ) {
    const updatedScheduleItems = scheduleItems.map((scheduleItem, index) => {
      if (index === position) {
        return { ...scheduleItem, [field]: value };
      }

      return scheduleItems;
    });

    setScheduleItems(updatedScheduleItems);
  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...